net-sysfs.c 33 KB

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