net-sysfs.c 37 KB

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