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