net-sysfs.c 32 KB

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