net-sysfs.c 34 KB

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