net-sysfs.c 32 KB

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