net-sysfs.c 34 KB

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