net-sysfs.c 36 KB

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