net-sysfs.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. /*
  2. * net-sysfs.c - network device class and attributes
  3. *
  4. * Copyright (c) 2003 Stephen Hemminger <shemminger@osdl.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/capability.h>
  12. #include <linux/kernel.h>
  13. #include <linux/netdevice.h>
  14. #include <net/switchdev.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/nsproxy.h>
  19. #include <net/sock.h>
  20. #include <net/net_namespace.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/export.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/of.h>
  27. #include <linux/of_net.h>
  28. #include "net-sysfs.h"
  29. #ifdef CONFIG_SYSFS
  30. static const char fmt_hex[] = "%#x\n";
  31. static const char fmt_dec[] = "%d\n";
  32. static const char fmt_ulong[] = "%lu\n";
  33. static const char fmt_u64[] = "%llu\n";
  34. static inline int dev_isalive(const struct net_device *dev)
  35. {
  36. return dev->reg_state <= NETREG_REGISTERED;
  37. }
  38. /* use same locking rules as GIF* ioctl's */
  39. static ssize_t netdev_show(const struct device *dev,
  40. struct device_attribute *attr, char *buf,
  41. ssize_t (*format)(const struct net_device *, char *))
  42. {
  43. struct net_device *ndev = to_net_dev(dev);
  44. ssize_t ret = -EINVAL;
  45. read_lock(&dev_base_lock);
  46. if (dev_isalive(ndev))
  47. ret = (*format)(ndev, buf);
  48. read_unlock(&dev_base_lock);
  49. return ret;
  50. }
  51. /* generate a show function for simple field */
  52. #define NETDEVICE_SHOW(field, format_string) \
  53. static ssize_t format_##field(const struct net_device *dev, char *buf) \
  54. { \
  55. return sprintf(buf, format_string, dev->field); \
  56. } \
  57. static ssize_t field##_show(struct device *dev, \
  58. struct device_attribute *attr, char *buf) \
  59. { \
  60. return netdev_show(dev, attr, buf, format_##field); \
  61. } \
  62. #define NETDEVICE_SHOW_RO(field, format_string) \
  63. NETDEVICE_SHOW(field, format_string); \
  64. static DEVICE_ATTR_RO(field)
  65. #define NETDEVICE_SHOW_RW(field, format_string) \
  66. NETDEVICE_SHOW(field, format_string); \
  67. static DEVICE_ATTR_RW(field)
  68. /* use same locking and permission rules as SIF* ioctl's */
  69. static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  70. const char *buf, size_t len,
  71. int (*set)(struct net_device *, unsigned long))
  72. {
  73. struct net_device *netdev = to_net_dev(dev);
  74. struct net *net = dev_net(netdev);
  75. unsigned long new;
  76. int ret = -EINVAL;
  77. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  78. return -EPERM;
  79. ret = kstrtoul(buf, 0, &new);
  80. if (ret)
  81. goto err;
  82. if (!rtnl_trylock())
  83. return restart_syscall();
  84. if (dev_isalive(netdev)) {
  85. if ((ret = (*set)(netdev, new)) == 0)
  86. ret = len;
  87. }
  88. rtnl_unlock();
  89. err:
  90. return ret;
  91. }
  92. NETDEVICE_SHOW_RO(dev_id, fmt_hex);
  93. NETDEVICE_SHOW_RO(dev_port, fmt_dec);
  94. NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
  95. NETDEVICE_SHOW_RO(addr_len, fmt_dec);
  96. NETDEVICE_SHOW_RO(ifindex, fmt_dec);
  97. NETDEVICE_SHOW_RO(type, fmt_dec);
  98. NETDEVICE_SHOW_RO(link_mode, fmt_dec);
  99. static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
  100. char *buf)
  101. {
  102. struct net_device *ndev = to_net_dev(dev);
  103. return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
  104. }
  105. static DEVICE_ATTR_RO(iflink);
  106. static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
  107. {
  108. return sprintf(buf, fmt_dec, dev->name_assign_type);
  109. }
  110. static ssize_t name_assign_type_show(struct device *dev,
  111. struct device_attribute *attr,
  112. char *buf)
  113. {
  114. struct net_device *ndev = to_net_dev(dev);
  115. ssize_t ret = -EINVAL;
  116. if (ndev->name_assign_type != NET_NAME_UNKNOWN)
  117. ret = netdev_show(dev, attr, buf, format_name_assign_type);
  118. return ret;
  119. }
  120. static DEVICE_ATTR_RO(name_assign_type);
  121. /* use same locking rules as GIFHWADDR ioctl's */
  122. static ssize_t address_show(struct device *dev, struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct net_device *ndev = to_net_dev(dev);
  126. ssize_t ret = -EINVAL;
  127. read_lock(&dev_base_lock);
  128. if (dev_isalive(ndev))
  129. ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
  130. read_unlock(&dev_base_lock);
  131. return ret;
  132. }
  133. static DEVICE_ATTR_RO(address);
  134. static ssize_t broadcast_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. struct net_device *ndev = to_net_dev(dev);
  138. if (dev_isalive(ndev))
  139. return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
  140. return -EINVAL;
  141. }
  142. static DEVICE_ATTR_RO(broadcast);
  143. static int change_carrier(struct net_device *dev, unsigned long new_carrier)
  144. {
  145. if (!netif_running(dev))
  146. return -EINVAL;
  147. return dev_change_carrier(dev, (bool) new_carrier);
  148. }
  149. static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
  150. const char *buf, size_t len)
  151. {
  152. return netdev_store(dev, attr, buf, len, change_carrier);
  153. }
  154. static ssize_t carrier_show(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct net_device *netdev = to_net_dev(dev);
  158. if (netif_running(netdev)) {
  159. return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
  160. }
  161. return -EINVAL;
  162. }
  163. static DEVICE_ATTR_RW(carrier);
  164. static ssize_t speed_show(struct device *dev,
  165. struct device_attribute *attr, char *buf)
  166. {
  167. struct net_device *netdev = to_net_dev(dev);
  168. int ret = -EINVAL;
  169. if (!rtnl_trylock())
  170. return restart_syscall();
  171. if (netif_running(netdev)) {
  172. struct ethtool_link_ksettings cmd;
  173. if (!__ethtool_get_link_ksettings(netdev, &cmd))
  174. ret = sprintf(buf, fmt_dec, cmd.base.speed);
  175. }
  176. rtnl_unlock();
  177. return ret;
  178. }
  179. static DEVICE_ATTR_RO(speed);
  180. static ssize_t duplex_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. struct net_device *netdev = to_net_dev(dev);
  184. int ret = -EINVAL;
  185. if (!rtnl_trylock())
  186. return restart_syscall();
  187. if (netif_running(netdev)) {
  188. struct ethtool_link_ksettings cmd;
  189. if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
  190. const char *duplex;
  191. switch (cmd.base.duplex) {
  192. case DUPLEX_HALF:
  193. duplex = "half";
  194. break;
  195. case DUPLEX_FULL:
  196. duplex = "full";
  197. break;
  198. default:
  199. duplex = "unknown";
  200. break;
  201. }
  202. ret = sprintf(buf, "%s\n", duplex);
  203. }
  204. }
  205. rtnl_unlock();
  206. return ret;
  207. }
  208. static DEVICE_ATTR_RO(duplex);
  209. static ssize_t dormant_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct net_device *netdev = to_net_dev(dev);
  213. if (netif_running(netdev))
  214. return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
  215. return -EINVAL;
  216. }
  217. static DEVICE_ATTR_RO(dormant);
  218. static const char *const operstates[] = {
  219. "unknown",
  220. "notpresent", /* currently unused */
  221. "down",
  222. "lowerlayerdown",
  223. "testing", /* currently unused */
  224. "dormant",
  225. "up"
  226. };
  227. static ssize_t operstate_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. const struct net_device *netdev = to_net_dev(dev);
  231. unsigned char operstate;
  232. read_lock(&dev_base_lock);
  233. operstate = netdev->operstate;
  234. if (!netif_running(netdev))
  235. operstate = IF_OPER_DOWN;
  236. read_unlock(&dev_base_lock);
  237. if (operstate >= ARRAY_SIZE(operstates))
  238. return -EINVAL; /* should not happen */
  239. return sprintf(buf, "%s\n", operstates[operstate]);
  240. }
  241. static DEVICE_ATTR_RO(operstate);
  242. static ssize_t carrier_changes_show(struct device *dev,
  243. struct device_attribute *attr,
  244. char *buf)
  245. {
  246. struct net_device *netdev = to_net_dev(dev);
  247. return sprintf(buf, fmt_dec,
  248. atomic_read(&netdev->carrier_changes));
  249. }
  250. static DEVICE_ATTR_RO(carrier_changes);
  251. /* read-write attributes */
  252. static int change_mtu(struct net_device *dev, unsigned long new_mtu)
  253. {
  254. return dev_set_mtu(dev, (int) new_mtu);
  255. }
  256. static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
  257. const char *buf, size_t len)
  258. {
  259. return netdev_store(dev, attr, buf, len, change_mtu);
  260. }
  261. NETDEVICE_SHOW_RW(mtu, fmt_dec);
  262. static int change_flags(struct net_device *dev, unsigned long new_flags)
  263. {
  264. return dev_change_flags(dev, (unsigned int) new_flags);
  265. }
  266. static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
  267. const char *buf, size_t len)
  268. {
  269. return netdev_store(dev, attr, buf, len, change_flags);
  270. }
  271. NETDEVICE_SHOW_RW(flags, fmt_hex);
  272. static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
  273. {
  274. unsigned int orig_len = dev->tx_queue_len;
  275. int res;
  276. if (new_len != (unsigned int)new_len)
  277. return -ERANGE;
  278. if (new_len != orig_len) {
  279. dev->tx_queue_len = new_len;
  280. res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
  281. res = notifier_to_errno(res);
  282. if (res) {
  283. netdev_err(dev,
  284. "refused to change device tx_queue_len\n");
  285. dev->tx_queue_len = orig_len;
  286. return -EFAULT;
  287. }
  288. }
  289. return 0;
  290. }
  291. static ssize_t tx_queue_len_store(struct device *dev,
  292. struct device_attribute *attr,
  293. const char *buf, size_t len)
  294. {
  295. if (!capable(CAP_NET_ADMIN))
  296. return -EPERM;
  297. return netdev_store(dev, attr, buf, len, change_tx_queue_len);
  298. }
  299. NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
  300. static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
  301. {
  302. dev->gro_flush_timeout = val;
  303. return 0;
  304. }
  305. static ssize_t gro_flush_timeout_store(struct device *dev,
  306. struct device_attribute *attr,
  307. const char *buf, size_t len)
  308. {
  309. if (!capable(CAP_NET_ADMIN))
  310. return -EPERM;
  311. return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
  312. }
  313. NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
  314. static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
  315. const char *buf, size_t len)
  316. {
  317. struct net_device *netdev = to_net_dev(dev);
  318. struct net *net = dev_net(netdev);
  319. size_t count = len;
  320. ssize_t ret;
  321. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  322. return -EPERM;
  323. /* ignore trailing newline */
  324. if (len > 0 && buf[len - 1] == '\n')
  325. --count;
  326. if (!rtnl_trylock())
  327. return restart_syscall();
  328. ret = dev_set_alias(netdev, buf, count);
  329. rtnl_unlock();
  330. return ret < 0 ? ret : len;
  331. }
  332. static ssize_t ifalias_show(struct device *dev,
  333. struct device_attribute *attr, char *buf)
  334. {
  335. const struct net_device *netdev = to_net_dev(dev);
  336. ssize_t ret = 0;
  337. if (!rtnl_trylock())
  338. return restart_syscall();
  339. if (netdev->ifalias)
  340. ret = sprintf(buf, "%s\n", netdev->ifalias);
  341. rtnl_unlock();
  342. return ret;
  343. }
  344. static DEVICE_ATTR_RW(ifalias);
  345. static int change_group(struct net_device *dev, unsigned long new_group)
  346. {
  347. dev_set_group(dev, (int) new_group);
  348. return 0;
  349. }
  350. static ssize_t group_store(struct device *dev, struct device_attribute *attr,
  351. const char *buf, size_t len)
  352. {
  353. return netdev_store(dev, attr, buf, len, change_group);
  354. }
  355. NETDEVICE_SHOW(group, fmt_dec);
  356. static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
  357. static int change_proto_down(struct net_device *dev, unsigned long proto_down)
  358. {
  359. return dev_change_proto_down(dev, (bool) proto_down);
  360. }
  361. static ssize_t proto_down_store(struct device *dev,
  362. struct device_attribute *attr,
  363. const char *buf, size_t len)
  364. {
  365. return netdev_store(dev, attr, buf, len, change_proto_down);
  366. }
  367. NETDEVICE_SHOW_RW(proto_down, fmt_dec);
  368. static ssize_t phys_port_id_show(struct device *dev,
  369. struct device_attribute *attr, char *buf)
  370. {
  371. struct net_device *netdev = to_net_dev(dev);
  372. ssize_t ret = -EINVAL;
  373. if (!rtnl_trylock())
  374. return restart_syscall();
  375. if (dev_isalive(netdev)) {
  376. struct netdev_phys_item_id ppid;
  377. ret = dev_get_phys_port_id(netdev, &ppid);
  378. if (!ret)
  379. ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
  380. }
  381. rtnl_unlock();
  382. return ret;
  383. }
  384. static DEVICE_ATTR_RO(phys_port_id);
  385. static ssize_t phys_port_name_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct net_device *netdev = to_net_dev(dev);
  389. ssize_t ret = -EINVAL;
  390. if (!rtnl_trylock())
  391. return restart_syscall();
  392. if (dev_isalive(netdev)) {
  393. char name[IFNAMSIZ];
  394. ret = dev_get_phys_port_name(netdev, name, sizeof(name));
  395. if (!ret)
  396. ret = sprintf(buf, "%s\n", name);
  397. }
  398. rtnl_unlock();
  399. return ret;
  400. }
  401. static DEVICE_ATTR_RO(phys_port_name);
  402. static ssize_t phys_switch_id_show(struct device *dev,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. struct net_device *netdev = to_net_dev(dev);
  406. ssize_t ret = -EINVAL;
  407. if (!rtnl_trylock())
  408. return restart_syscall();
  409. if (dev_isalive(netdev)) {
  410. struct switchdev_attr attr = {
  411. .orig_dev = netdev,
  412. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  413. .flags = SWITCHDEV_F_NO_RECURSE,
  414. };
  415. ret = switchdev_port_attr_get(netdev, &attr);
  416. if (!ret)
  417. ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
  418. attr.u.ppid.id);
  419. }
  420. rtnl_unlock();
  421. return ret;
  422. }
  423. static DEVICE_ATTR_RO(phys_switch_id);
  424. static struct attribute *net_class_attrs[] = {
  425. &dev_attr_netdev_group.attr,
  426. &dev_attr_type.attr,
  427. &dev_attr_dev_id.attr,
  428. &dev_attr_dev_port.attr,
  429. &dev_attr_iflink.attr,
  430. &dev_attr_ifindex.attr,
  431. &dev_attr_name_assign_type.attr,
  432. &dev_attr_addr_assign_type.attr,
  433. &dev_attr_addr_len.attr,
  434. &dev_attr_link_mode.attr,
  435. &dev_attr_address.attr,
  436. &dev_attr_broadcast.attr,
  437. &dev_attr_speed.attr,
  438. &dev_attr_duplex.attr,
  439. &dev_attr_dormant.attr,
  440. &dev_attr_operstate.attr,
  441. &dev_attr_carrier_changes.attr,
  442. &dev_attr_ifalias.attr,
  443. &dev_attr_carrier.attr,
  444. &dev_attr_mtu.attr,
  445. &dev_attr_flags.attr,
  446. &dev_attr_tx_queue_len.attr,
  447. &dev_attr_gro_flush_timeout.attr,
  448. &dev_attr_phys_port_id.attr,
  449. &dev_attr_phys_port_name.attr,
  450. &dev_attr_phys_switch_id.attr,
  451. &dev_attr_proto_down.attr,
  452. NULL,
  453. };
  454. ATTRIBUTE_GROUPS(net_class);
  455. /* Show a given an attribute in the statistics group */
  456. static ssize_t netstat_show(const struct device *d,
  457. struct device_attribute *attr, char *buf,
  458. unsigned long offset)
  459. {
  460. struct net_device *dev = to_net_dev(d);
  461. ssize_t ret = -EINVAL;
  462. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  463. offset % sizeof(u64) != 0);
  464. read_lock(&dev_base_lock);
  465. if (dev_isalive(dev)) {
  466. struct rtnl_link_stats64 temp;
  467. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  468. ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
  469. }
  470. read_unlock(&dev_base_lock);
  471. return ret;
  472. }
  473. /* generate a read-only statistics attribute */
  474. #define NETSTAT_ENTRY(name) \
  475. static ssize_t name##_show(struct device *d, \
  476. struct device_attribute *attr, char *buf) \
  477. { \
  478. return netstat_show(d, attr, buf, \
  479. offsetof(struct rtnl_link_stats64, name)); \
  480. } \
  481. static DEVICE_ATTR_RO(name)
  482. NETSTAT_ENTRY(rx_packets);
  483. NETSTAT_ENTRY(tx_packets);
  484. NETSTAT_ENTRY(rx_bytes);
  485. NETSTAT_ENTRY(tx_bytes);
  486. NETSTAT_ENTRY(rx_errors);
  487. NETSTAT_ENTRY(tx_errors);
  488. NETSTAT_ENTRY(rx_dropped);
  489. NETSTAT_ENTRY(tx_dropped);
  490. NETSTAT_ENTRY(multicast);
  491. NETSTAT_ENTRY(collisions);
  492. NETSTAT_ENTRY(rx_length_errors);
  493. NETSTAT_ENTRY(rx_over_errors);
  494. NETSTAT_ENTRY(rx_crc_errors);
  495. NETSTAT_ENTRY(rx_frame_errors);
  496. NETSTAT_ENTRY(rx_fifo_errors);
  497. NETSTAT_ENTRY(rx_missed_errors);
  498. NETSTAT_ENTRY(tx_aborted_errors);
  499. NETSTAT_ENTRY(tx_carrier_errors);
  500. NETSTAT_ENTRY(tx_fifo_errors);
  501. NETSTAT_ENTRY(tx_heartbeat_errors);
  502. NETSTAT_ENTRY(tx_window_errors);
  503. NETSTAT_ENTRY(rx_compressed);
  504. NETSTAT_ENTRY(tx_compressed);
  505. NETSTAT_ENTRY(rx_nohandler);
  506. static struct attribute *netstat_attrs[] = {
  507. &dev_attr_rx_packets.attr,
  508. &dev_attr_tx_packets.attr,
  509. &dev_attr_rx_bytes.attr,
  510. &dev_attr_tx_bytes.attr,
  511. &dev_attr_rx_errors.attr,
  512. &dev_attr_tx_errors.attr,
  513. &dev_attr_rx_dropped.attr,
  514. &dev_attr_tx_dropped.attr,
  515. &dev_attr_multicast.attr,
  516. &dev_attr_collisions.attr,
  517. &dev_attr_rx_length_errors.attr,
  518. &dev_attr_rx_over_errors.attr,
  519. &dev_attr_rx_crc_errors.attr,
  520. &dev_attr_rx_frame_errors.attr,
  521. &dev_attr_rx_fifo_errors.attr,
  522. &dev_attr_rx_missed_errors.attr,
  523. &dev_attr_tx_aborted_errors.attr,
  524. &dev_attr_tx_carrier_errors.attr,
  525. &dev_attr_tx_fifo_errors.attr,
  526. &dev_attr_tx_heartbeat_errors.attr,
  527. &dev_attr_tx_window_errors.attr,
  528. &dev_attr_rx_compressed.attr,
  529. &dev_attr_tx_compressed.attr,
  530. &dev_attr_rx_nohandler.attr,
  531. NULL
  532. };
  533. static const struct attribute_group netstat_group = {
  534. .name = "statistics",
  535. .attrs = netstat_attrs,
  536. };
  537. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  538. static struct attribute *wireless_attrs[] = {
  539. NULL
  540. };
  541. static const struct attribute_group wireless_group = {
  542. .name = "wireless",
  543. .attrs = wireless_attrs,
  544. };
  545. #endif
  546. #else /* CONFIG_SYSFS */
  547. #define net_class_groups NULL
  548. #endif /* CONFIG_SYSFS */
  549. #ifdef CONFIG_SYSFS
  550. #define to_rx_queue_attr(_attr) container_of(_attr, \
  551. struct rx_queue_attribute, attr)
  552. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  553. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  554. char *buf)
  555. {
  556. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  557. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  558. if (!attribute->show)
  559. return -EIO;
  560. return attribute->show(queue, attribute, buf);
  561. }
  562. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  563. const char *buf, size_t count)
  564. {
  565. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  566. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  567. if (!attribute->store)
  568. return -EIO;
  569. return attribute->store(queue, attribute, buf, count);
  570. }
  571. static const struct sysfs_ops rx_queue_sysfs_ops = {
  572. .show = rx_queue_attr_show,
  573. .store = rx_queue_attr_store,
  574. };
  575. #ifdef CONFIG_RPS
  576. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  577. struct rx_queue_attribute *attribute, char *buf)
  578. {
  579. struct rps_map *map;
  580. cpumask_var_t mask;
  581. int i, len;
  582. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  583. return -ENOMEM;
  584. rcu_read_lock();
  585. map = rcu_dereference(queue->rps_map);
  586. if (map)
  587. for (i = 0; i < map->len; i++)
  588. cpumask_set_cpu(map->cpus[i], mask);
  589. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  590. rcu_read_unlock();
  591. free_cpumask_var(mask);
  592. return len < PAGE_SIZE ? len : -EINVAL;
  593. }
  594. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  595. struct rx_queue_attribute *attribute,
  596. const char *buf, size_t len)
  597. {
  598. struct rps_map *old_map, *map;
  599. cpumask_var_t mask;
  600. int err, cpu, i;
  601. static DEFINE_MUTEX(rps_map_mutex);
  602. if (!capable(CAP_NET_ADMIN))
  603. return -EPERM;
  604. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  605. return -ENOMEM;
  606. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  607. if (err) {
  608. free_cpumask_var(mask);
  609. return err;
  610. }
  611. map = kzalloc(max_t(unsigned int,
  612. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  613. GFP_KERNEL);
  614. if (!map) {
  615. free_cpumask_var(mask);
  616. return -ENOMEM;
  617. }
  618. i = 0;
  619. for_each_cpu_and(cpu, mask, cpu_online_mask)
  620. map->cpus[i++] = cpu;
  621. if (i)
  622. map->len = i;
  623. else {
  624. kfree(map);
  625. map = NULL;
  626. }
  627. mutex_lock(&rps_map_mutex);
  628. old_map = rcu_dereference_protected(queue->rps_map,
  629. mutex_is_locked(&rps_map_mutex));
  630. rcu_assign_pointer(queue->rps_map, map);
  631. if (map)
  632. static_key_slow_inc(&rps_needed);
  633. if (old_map)
  634. static_key_slow_dec(&rps_needed);
  635. mutex_unlock(&rps_map_mutex);
  636. if (old_map)
  637. kfree_rcu(old_map, rcu);
  638. free_cpumask_var(mask);
  639. return len;
  640. }
  641. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  642. struct rx_queue_attribute *attr,
  643. char *buf)
  644. {
  645. struct rps_dev_flow_table *flow_table;
  646. unsigned long val = 0;
  647. rcu_read_lock();
  648. flow_table = rcu_dereference(queue->rps_flow_table);
  649. if (flow_table)
  650. val = (unsigned long)flow_table->mask + 1;
  651. rcu_read_unlock();
  652. return sprintf(buf, "%lu\n", val);
  653. }
  654. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  655. {
  656. struct rps_dev_flow_table *table = container_of(rcu,
  657. struct rps_dev_flow_table, rcu);
  658. vfree(table);
  659. }
  660. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  661. struct rx_queue_attribute *attr,
  662. const char *buf, size_t len)
  663. {
  664. unsigned long mask, count;
  665. struct rps_dev_flow_table *table, *old_table;
  666. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  667. int rc;
  668. if (!capable(CAP_NET_ADMIN))
  669. return -EPERM;
  670. rc = kstrtoul(buf, 0, &count);
  671. if (rc < 0)
  672. return rc;
  673. if (count) {
  674. mask = count - 1;
  675. /* mask = roundup_pow_of_two(count) - 1;
  676. * without overflows...
  677. */
  678. while ((mask | (mask >> 1)) != mask)
  679. mask |= (mask >> 1);
  680. /* On 64 bit arches, must check mask fits in table->mask (u32),
  681. * and on 32bit arches, must check
  682. * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
  683. */
  684. #if BITS_PER_LONG > 32
  685. if (mask > (unsigned long)(u32)mask)
  686. return -EINVAL;
  687. #else
  688. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  689. / sizeof(struct rps_dev_flow)) {
  690. /* Enforce a limit to prevent overflow */
  691. return -EINVAL;
  692. }
  693. #endif
  694. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  695. if (!table)
  696. return -ENOMEM;
  697. table->mask = mask;
  698. for (count = 0; count <= mask; count++)
  699. table->flows[count].cpu = RPS_NO_CPU;
  700. } else
  701. table = NULL;
  702. spin_lock(&rps_dev_flow_lock);
  703. old_table = rcu_dereference_protected(queue->rps_flow_table,
  704. lockdep_is_held(&rps_dev_flow_lock));
  705. rcu_assign_pointer(queue->rps_flow_table, table);
  706. spin_unlock(&rps_dev_flow_lock);
  707. if (old_table)
  708. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  709. return len;
  710. }
  711. static struct rx_queue_attribute rps_cpus_attribute =
  712. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  713. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  714. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  715. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  716. #endif /* CONFIG_RPS */
  717. static struct attribute *rx_queue_default_attrs[] = {
  718. #ifdef CONFIG_RPS
  719. &rps_cpus_attribute.attr,
  720. &rps_dev_flow_table_cnt_attribute.attr,
  721. #endif
  722. NULL
  723. };
  724. static void rx_queue_release(struct kobject *kobj)
  725. {
  726. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  727. #ifdef CONFIG_RPS
  728. struct rps_map *map;
  729. struct rps_dev_flow_table *flow_table;
  730. map = rcu_dereference_protected(queue->rps_map, 1);
  731. if (map) {
  732. RCU_INIT_POINTER(queue->rps_map, NULL);
  733. kfree_rcu(map, rcu);
  734. }
  735. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  736. if (flow_table) {
  737. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  738. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  739. }
  740. #endif
  741. memset(kobj, 0, sizeof(*kobj));
  742. dev_put(queue->dev);
  743. }
  744. static const void *rx_queue_namespace(struct kobject *kobj)
  745. {
  746. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  747. struct device *dev = &queue->dev->dev;
  748. const void *ns = NULL;
  749. if (dev->class && dev->class->ns_type)
  750. ns = dev->class->namespace(dev);
  751. return ns;
  752. }
  753. static struct kobj_type rx_queue_ktype = {
  754. .sysfs_ops = &rx_queue_sysfs_ops,
  755. .release = rx_queue_release,
  756. .default_attrs = rx_queue_default_attrs,
  757. .namespace = rx_queue_namespace
  758. };
  759. static int rx_queue_add_kobject(struct net_device *dev, int index)
  760. {
  761. struct netdev_rx_queue *queue = dev->_rx + index;
  762. struct kobject *kobj = &queue->kobj;
  763. int error = 0;
  764. kobj->kset = dev->queues_kset;
  765. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  766. "rx-%u", index);
  767. if (error)
  768. goto exit;
  769. if (dev->sysfs_rx_queue_group) {
  770. error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
  771. if (error)
  772. goto exit;
  773. }
  774. kobject_uevent(kobj, KOBJ_ADD);
  775. dev_hold(queue->dev);
  776. return error;
  777. exit:
  778. kobject_put(kobj);
  779. return error;
  780. }
  781. #endif /* CONFIG_SYSFS */
  782. int
  783. net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  784. {
  785. #ifdef CONFIG_SYSFS
  786. int i;
  787. int error = 0;
  788. #ifndef CONFIG_RPS
  789. if (!dev->sysfs_rx_queue_group)
  790. return 0;
  791. #endif
  792. for (i = old_num; i < new_num; i++) {
  793. error = rx_queue_add_kobject(dev, i);
  794. if (error) {
  795. new_num = old_num;
  796. break;
  797. }
  798. }
  799. while (--i >= new_num) {
  800. struct kobject *kobj = &dev->_rx[i].kobj;
  801. if (!atomic_read(&dev_net(dev)->count))
  802. kobj->uevent_suppress = 1;
  803. if (dev->sysfs_rx_queue_group)
  804. sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
  805. kobject_put(kobj);
  806. }
  807. return error;
  808. #else
  809. return 0;
  810. #endif
  811. }
  812. #ifdef CONFIG_SYSFS
  813. /*
  814. * netdev_queue sysfs structures and functions.
  815. */
  816. struct netdev_queue_attribute {
  817. struct attribute attr;
  818. ssize_t (*show)(struct netdev_queue *queue,
  819. struct netdev_queue_attribute *attr, char *buf);
  820. ssize_t (*store)(struct netdev_queue *queue,
  821. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  822. };
  823. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  824. struct netdev_queue_attribute, attr)
  825. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  826. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  827. struct attribute *attr, char *buf)
  828. {
  829. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  830. struct netdev_queue *queue = to_netdev_queue(kobj);
  831. if (!attribute->show)
  832. return -EIO;
  833. return attribute->show(queue, attribute, buf);
  834. }
  835. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  836. struct attribute *attr,
  837. const char *buf, size_t count)
  838. {
  839. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  840. struct netdev_queue *queue = to_netdev_queue(kobj);
  841. if (!attribute->store)
  842. return -EIO;
  843. return attribute->store(queue, attribute, buf, count);
  844. }
  845. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  846. .show = netdev_queue_attr_show,
  847. .store = netdev_queue_attr_store,
  848. };
  849. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  850. struct netdev_queue_attribute *attribute,
  851. char *buf)
  852. {
  853. unsigned long trans_timeout;
  854. spin_lock_irq(&queue->_xmit_lock);
  855. trans_timeout = queue->trans_timeout;
  856. spin_unlock_irq(&queue->_xmit_lock);
  857. return sprintf(buf, "%lu", trans_timeout);
  858. }
  859. static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  860. {
  861. struct net_device *dev = queue->dev;
  862. unsigned int i;
  863. i = queue - dev->_tx;
  864. BUG_ON(i >= dev->num_tx_queues);
  865. return i;
  866. }
  867. static ssize_t show_traffic_class(struct netdev_queue *queue,
  868. struct netdev_queue_attribute *attribute,
  869. char *buf)
  870. {
  871. struct net_device *dev = queue->dev;
  872. int index = get_netdev_queue_index(queue);
  873. int tc = netdev_txq_to_tc(dev, index);
  874. if (tc < 0)
  875. return -EINVAL;
  876. return sprintf(buf, "%u\n", tc);
  877. }
  878. #ifdef CONFIG_XPS
  879. static ssize_t show_tx_maxrate(struct netdev_queue *queue,
  880. struct netdev_queue_attribute *attribute,
  881. char *buf)
  882. {
  883. return sprintf(buf, "%lu\n", queue->tx_maxrate);
  884. }
  885. static ssize_t set_tx_maxrate(struct netdev_queue *queue,
  886. struct netdev_queue_attribute *attribute,
  887. const char *buf, size_t len)
  888. {
  889. struct net_device *dev = queue->dev;
  890. int err, index = get_netdev_queue_index(queue);
  891. u32 rate = 0;
  892. err = kstrtou32(buf, 10, &rate);
  893. if (err < 0)
  894. return err;
  895. if (!rtnl_trylock())
  896. return restart_syscall();
  897. err = -EOPNOTSUPP;
  898. if (dev->netdev_ops->ndo_set_tx_maxrate)
  899. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  900. rtnl_unlock();
  901. if (!err) {
  902. queue->tx_maxrate = rate;
  903. return len;
  904. }
  905. return err;
  906. }
  907. static struct netdev_queue_attribute queue_tx_maxrate =
  908. __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
  909. show_tx_maxrate, set_tx_maxrate);
  910. #endif
  911. static struct netdev_queue_attribute queue_trans_timeout =
  912. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  913. static struct netdev_queue_attribute queue_traffic_class =
  914. __ATTR(traffic_class, S_IRUGO, show_traffic_class, NULL);
  915. #ifdef CONFIG_BQL
  916. /*
  917. * Byte queue limits sysfs structures and functions.
  918. */
  919. static ssize_t bql_show(char *buf, unsigned int value)
  920. {
  921. return sprintf(buf, "%u\n", value);
  922. }
  923. static ssize_t bql_set(const char *buf, const size_t count,
  924. unsigned int *pvalue)
  925. {
  926. unsigned int value;
  927. int err;
  928. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  929. value = DQL_MAX_LIMIT;
  930. else {
  931. err = kstrtouint(buf, 10, &value);
  932. if (err < 0)
  933. return err;
  934. if (value > DQL_MAX_LIMIT)
  935. return -EINVAL;
  936. }
  937. *pvalue = value;
  938. return count;
  939. }
  940. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  941. struct netdev_queue_attribute *attr,
  942. char *buf)
  943. {
  944. struct dql *dql = &queue->dql;
  945. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  946. }
  947. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  948. struct netdev_queue_attribute *attribute,
  949. const char *buf, size_t len)
  950. {
  951. struct dql *dql = &queue->dql;
  952. unsigned int value;
  953. int err;
  954. err = kstrtouint(buf, 10, &value);
  955. if (err < 0)
  956. return err;
  957. dql->slack_hold_time = msecs_to_jiffies(value);
  958. return len;
  959. }
  960. static struct netdev_queue_attribute bql_hold_time_attribute =
  961. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  962. bql_set_hold_time);
  963. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  964. struct netdev_queue_attribute *attr,
  965. char *buf)
  966. {
  967. struct dql *dql = &queue->dql;
  968. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  969. }
  970. static struct netdev_queue_attribute bql_inflight_attribute =
  971. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  972. #define BQL_ATTR(NAME, FIELD) \
  973. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  974. struct netdev_queue_attribute *attr, \
  975. char *buf) \
  976. { \
  977. return bql_show(buf, queue->dql.FIELD); \
  978. } \
  979. \
  980. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  981. struct netdev_queue_attribute *attr, \
  982. const char *buf, size_t len) \
  983. { \
  984. return bql_set(buf, len, &queue->dql.FIELD); \
  985. } \
  986. \
  987. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  988. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  989. bql_set_ ## NAME);
  990. BQL_ATTR(limit, limit)
  991. BQL_ATTR(limit_max, max_limit)
  992. BQL_ATTR(limit_min, min_limit)
  993. static struct attribute *dql_attrs[] = {
  994. &bql_limit_attribute.attr,
  995. &bql_limit_max_attribute.attr,
  996. &bql_limit_min_attribute.attr,
  997. &bql_hold_time_attribute.attr,
  998. &bql_inflight_attribute.attr,
  999. NULL
  1000. };
  1001. static const struct attribute_group dql_group = {
  1002. .name = "byte_queue_limits",
  1003. .attrs = dql_attrs,
  1004. };
  1005. #endif /* CONFIG_BQL */
  1006. #ifdef CONFIG_XPS
  1007. static ssize_t show_xps_map(struct netdev_queue *queue,
  1008. struct netdev_queue_attribute *attribute, char *buf)
  1009. {
  1010. struct net_device *dev = queue->dev;
  1011. int cpu, len, num_tc = 1, tc = 0;
  1012. struct xps_dev_maps *dev_maps;
  1013. cpumask_var_t mask;
  1014. unsigned long index;
  1015. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  1016. return -ENOMEM;
  1017. index = get_netdev_queue_index(queue);
  1018. if (dev->num_tc) {
  1019. num_tc = dev->num_tc;
  1020. tc = netdev_txq_to_tc(dev, index);
  1021. if (tc < 0)
  1022. return -EINVAL;
  1023. }
  1024. rcu_read_lock();
  1025. dev_maps = rcu_dereference(dev->xps_maps);
  1026. if (dev_maps) {
  1027. for_each_possible_cpu(cpu) {
  1028. int i, tci = cpu * num_tc + tc;
  1029. struct xps_map *map;
  1030. map = rcu_dereference(dev_maps->cpu_map[tci]);
  1031. if (!map)
  1032. continue;
  1033. for (i = map->len; i--;) {
  1034. if (map->queues[i] == index) {
  1035. cpumask_set_cpu(cpu, mask);
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. }
  1041. rcu_read_unlock();
  1042. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  1043. free_cpumask_var(mask);
  1044. return len < PAGE_SIZE ? len : -EINVAL;
  1045. }
  1046. static ssize_t store_xps_map(struct netdev_queue *queue,
  1047. struct netdev_queue_attribute *attribute,
  1048. const char *buf, size_t len)
  1049. {
  1050. struct net_device *dev = queue->dev;
  1051. unsigned long index;
  1052. cpumask_var_t mask;
  1053. int err;
  1054. if (!capable(CAP_NET_ADMIN))
  1055. return -EPERM;
  1056. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1057. return -ENOMEM;
  1058. index = get_netdev_queue_index(queue);
  1059. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1060. if (err) {
  1061. free_cpumask_var(mask);
  1062. return err;
  1063. }
  1064. err = netif_set_xps_queue(dev, mask, index);
  1065. free_cpumask_var(mask);
  1066. return err ? : len;
  1067. }
  1068. static struct netdev_queue_attribute xps_cpus_attribute =
  1069. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  1070. #endif /* CONFIG_XPS */
  1071. static struct attribute *netdev_queue_default_attrs[] = {
  1072. &queue_trans_timeout.attr,
  1073. &queue_traffic_class.attr,
  1074. #ifdef CONFIG_XPS
  1075. &xps_cpus_attribute.attr,
  1076. &queue_tx_maxrate.attr,
  1077. #endif
  1078. NULL
  1079. };
  1080. static void netdev_queue_release(struct kobject *kobj)
  1081. {
  1082. struct netdev_queue *queue = to_netdev_queue(kobj);
  1083. memset(kobj, 0, sizeof(*kobj));
  1084. dev_put(queue->dev);
  1085. }
  1086. static const void *netdev_queue_namespace(struct kobject *kobj)
  1087. {
  1088. struct netdev_queue *queue = to_netdev_queue(kobj);
  1089. struct device *dev = &queue->dev->dev;
  1090. const void *ns = NULL;
  1091. if (dev->class && dev->class->ns_type)
  1092. ns = dev->class->namespace(dev);
  1093. return ns;
  1094. }
  1095. static struct kobj_type netdev_queue_ktype = {
  1096. .sysfs_ops = &netdev_queue_sysfs_ops,
  1097. .release = netdev_queue_release,
  1098. .default_attrs = netdev_queue_default_attrs,
  1099. .namespace = netdev_queue_namespace,
  1100. };
  1101. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1102. {
  1103. struct netdev_queue *queue = dev->_tx + index;
  1104. struct kobject *kobj = &queue->kobj;
  1105. int error = 0;
  1106. kobj->kset = dev->queues_kset;
  1107. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1108. "tx-%u", index);
  1109. if (error)
  1110. goto exit;
  1111. #ifdef CONFIG_BQL
  1112. error = sysfs_create_group(kobj, &dql_group);
  1113. if (error)
  1114. goto exit;
  1115. #endif
  1116. kobject_uevent(kobj, KOBJ_ADD);
  1117. dev_hold(queue->dev);
  1118. return 0;
  1119. exit:
  1120. kobject_put(kobj);
  1121. return error;
  1122. }
  1123. #endif /* CONFIG_SYSFS */
  1124. int
  1125. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1126. {
  1127. #ifdef CONFIG_SYSFS
  1128. int i;
  1129. int error = 0;
  1130. for (i = old_num; i < new_num; i++) {
  1131. error = netdev_queue_add_kobject(dev, i);
  1132. if (error) {
  1133. new_num = old_num;
  1134. break;
  1135. }
  1136. }
  1137. while (--i >= new_num) {
  1138. struct netdev_queue *queue = dev->_tx + i;
  1139. if (!atomic_read(&dev_net(dev)->count))
  1140. queue->kobj.uevent_suppress = 1;
  1141. #ifdef CONFIG_BQL
  1142. sysfs_remove_group(&queue->kobj, &dql_group);
  1143. #endif
  1144. kobject_put(&queue->kobj);
  1145. }
  1146. return error;
  1147. #else
  1148. return 0;
  1149. #endif /* CONFIG_SYSFS */
  1150. }
  1151. static int register_queue_kobjects(struct net_device *dev)
  1152. {
  1153. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1154. #ifdef CONFIG_SYSFS
  1155. dev->queues_kset = kset_create_and_add("queues",
  1156. NULL, &dev->dev.kobj);
  1157. if (!dev->queues_kset)
  1158. return -ENOMEM;
  1159. real_rx = dev->real_num_rx_queues;
  1160. #endif
  1161. real_tx = dev->real_num_tx_queues;
  1162. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1163. if (error)
  1164. goto error;
  1165. rxq = real_rx;
  1166. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1167. if (error)
  1168. goto error;
  1169. txq = real_tx;
  1170. return 0;
  1171. error:
  1172. netdev_queue_update_kobjects(dev, txq, 0);
  1173. net_rx_queue_update_kobjects(dev, rxq, 0);
  1174. return error;
  1175. }
  1176. static void remove_queue_kobjects(struct net_device *dev)
  1177. {
  1178. int real_rx = 0, real_tx = 0;
  1179. #ifdef CONFIG_SYSFS
  1180. real_rx = dev->real_num_rx_queues;
  1181. #endif
  1182. real_tx = dev->real_num_tx_queues;
  1183. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1184. netdev_queue_update_kobjects(dev, real_tx, 0);
  1185. #ifdef CONFIG_SYSFS
  1186. kset_unregister(dev->queues_kset);
  1187. #endif
  1188. }
  1189. static bool net_current_may_mount(void)
  1190. {
  1191. struct net *net = current->nsproxy->net_ns;
  1192. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1193. }
  1194. static void *net_grab_current_ns(void)
  1195. {
  1196. struct net *ns = current->nsproxy->net_ns;
  1197. #ifdef CONFIG_NET_NS
  1198. if (ns)
  1199. refcount_inc(&ns->passive);
  1200. #endif
  1201. return ns;
  1202. }
  1203. static const void *net_initial_ns(void)
  1204. {
  1205. return &init_net;
  1206. }
  1207. static const void *net_netlink_ns(struct sock *sk)
  1208. {
  1209. return sock_net(sk);
  1210. }
  1211. struct kobj_ns_type_operations net_ns_type_operations = {
  1212. .type = KOBJ_NS_TYPE_NET,
  1213. .current_may_mount = net_current_may_mount,
  1214. .grab_current_ns = net_grab_current_ns,
  1215. .netlink_ns = net_netlink_ns,
  1216. .initial_ns = net_initial_ns,
  1217. .drop_ns = net_drop_ns,
  1218. };
  1219. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1220. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1221. {
  1222. struct net_device *dev = to_net_dev(d);
  1223. int retval;
  1224. /* pass interface to uevent. */
  1225. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1226. if (retval)
  1227. goto exit;
  1228. /* pass ifindex to uevent.
  1229. * ifindex is useful as it won't change (interface name may change)
  1230. * and is what RtNetlink uses natively. */
  1231. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1232. exit:
  1233. return retval;
  1234. }
  1235. /*
  1236. * netdev_release -- destroy and free a dead device.
  1237. * Called when last reference to device kobject is gone.
  1238. */
  1239. static void netdev_release(struct device *d)
  1240. {
  1241. struct net_device *dev = to_net_dev(d);
  1242. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1243. kfree(dev->ifalias);
  1244. netdev_freemem(dev);
  1245. }
  1246. static const void *net_namespace(struct device *d)
  1247. {
  1248. struct net_device *dev = to_net_dev(d);
  1249. return dev_net(dev);
  1250. }
  1251. static struct class net_class = {
  1252. .name = "net",
  1253. .dev_release = netdev_release,
  1254. .dev_groups = net_class_groups,
  1255. .dev_uevent = netdev_uevent,
  1256. .ns_type = &net_ns_type_operations,
  1257. .namespace = net_namespace,
  1258. };
  1259. #ifdef CONFIG_OF_NET
  1260. static int of_dev_node_match(struct device *dev, const void *data)
  1261. {
  1262. int ret = 0;
  1263. if (dev->parent)
  1264. ret = dev->parent->of_node == data;
  1265. return ret == 0 ? dev->of_node == data : ret;
  1266. }
  1267. /*
  1268. * of_find_net_device_by_node - lookup the net device for the device node
  1269. * @np: OF device node
  1270. *
  1271. * Looks up the net_device structure corresponding with the device node.
  1272. * If successful, returns a pointer to the net_device with the embedded
  1273. * struct device refcount incremented by one, or NULL on failure. The
  1274. * refcount must be dropped when done with the net_device.
  1275. */
  1276. struct net_device *of_find_net_device_by_node(struct device_node *np)
  1277. {
  1278. struct device *dev;
  1279. dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
  1280. if (!dev)
  1281. return NULL;
  1282. return to_net_dev(dev);
  1283. }
  1284. EXPORT_SYMBOL(of_find_net_device_by_node);
  1285. #endif
  1286. /* Delete sysfs entries but hold kobject reference until after all
  1287. * netdev references are gone.
  1288. */
  1289. void netdev_unregister_kobject(struct net_device *ndev)
  1290. {
  1291. struct device *dev = &(ndev->dev);
  1292. if (!atomic_read(&dev_net(ndev)->count))
  1293. dev_set_uevent_suppress(dev, 1);
  1294. kobject_get(&dev->kobj);
  1295. remove_queue_kobjects(ndev);
  1296. pm_runtime_set_memalloc_noio(dev, false);
  1297. device_del(dev);
  1298. }
  1299. /* Create sysfs entries for network device. */
  1300. int netdev_register_kobject(struct net_device *ndev)
  1301. {
  1302. struct device *dev = &(ndev->dev);
  1303. const struct attribute_group **groups = ndev->sysfs_groups;
  1304. int error = 0;
  1305. device_initialize(dev);
  1306. dev->class = &net_class;
  1307. dev->platform_data = ndev;
  1308. dev->groups = groups;
  1309. dev_set_name(dev, "%s", ndev->name);
  1310. #ifdef CONFIG_SYSFS
  1311. /* Allow for a device specific group */
  1312. if (*groups)
  1313. groups++;
  1314. *groups++ = &netstat_group;
  1315. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  1316. if (ndev->ieee80211_ptr)
  1317. *groups++ = &wireless_group;
  1318. #if IS_ENABLED(CONFIG_WIRELESS_EXT)
  1319. else if (ndev->wireless_handlers)
  1320. *groups++ = &wireless_group;
  1321. #endif
  1322. #endif
  1323. #endif /* CONFIG_SYSFS */
  1324. error = device_add(dev);
  1325. if (error)
  1326. return error;
  1327. error = register_queue_kobjects(ndev);
  1328. if (error) {
  1329. device_del(dev);
  1330. return error;
  1331. }
  1332. pm_runtime_set_memalloc_noio(dev, true);
  1333. return error;
  1334. }
  1335. int netdev_class_create_file_ns(struct class_attribute *class_attr,
  1336. const void *ns)
  1337. {
  1338. return class_create_file_ns(&net_class, class_attr, ns);
  1339. }
  1340. EXPORT_SYMBOL(netdev_class_create_file_ns);
  1341. void netdev_class_remove_file_ns(struct class_attribute *class_attr,
  1342. const void *ns)
  1343. {
  1344. class_remove_file_ns(&net_class, class_attr, ns);
  1345. }
  1346. EXPORT_SYMBOL(netdev_class_remove_file_ns);
  1347. int __init netdev_kobject_init(void)
  1348. {
  1349. kobj_ns_type_register(&net_ns_type_operations);
  1350. return class_register(&net_class);
  1351. }