net-sysfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 <linux/of_net.h>
  27. #include "net-sysfs.h"
  28. #ifdef CONFIG_SYSFS
  29. static const char fmt_hex[] = "%#x\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_link_ksettings cmd;
  172. if (!__ethtool_get_link_ksettings(netdev, &cmd))
  173. ret = sprintf(buf, fmt_dec, cmd.base.speed);
  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_link_ksettings cmd;
  188. if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
  189. const char *duplex;
  190. switch (cmd.base.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. .orig_dev = netdev,
  397. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  398. .flags = SWITCHDEV_F_NO_RECURSE,
  399. };
  400. ret = switchdev_port_attr_get(netdev, &attr);
  401. if (!ret)
  402. ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
  403. attr.u.ppid.id);
  404. }
  405. rtnl_unlock();
  406. return ret;
  407. }
  408. static DEVICE_ATTR_RO(phys_switch_id);
  409. static struct attribute *net_class_attrs[] = {
  410. &dev_attr_netdev_group.attr,
  411. &dev_attr_type.attr,
  412. &dev_attr_dev_id.attr,
  413. &dev_attr_dev_port.attr,
  414. &dev_attr_iflink.attr,
  415. &dev_attr_ifindex.attr,
  416. &dev_attr_name_assign_type.attr,
  417. &dev_attr_addr_assign_type.attr,
  418. &dev_attr_addr_len.attr,
  419. &dev_attr_link_mode.attr,
  420. &dev_attr_address.attr,
  421. &dev_attr_broadcast.attr,
  422. &dev_attr_speed.attr,
  423. &dev_attr_duplex.attr,
  424. &dev_attr_dormant.attr,
  425. &dev_attr_operstate.attr,
  426. &dev_attr_carrier_changes.attr,
  427. &dev_attr_ifalias.attr,
  428. &dev_attr_carrier.attr,
  429. &dev_attr_mtu.attr,
  430. &dev_attr_flags.attr,
  431. &dev_attr_tx_queue_len.attr,
  432. &dev_attr_gro_flush_timeout.attr,
  433. &dev_attr_phys_port_id.attr,
  434. &dev_attr_phys_port_name.attr,
  435. &dev_attr_phys_switch_id.attr,
  436. &dev_attr_proto_down.attr,
  437. NULL,
  438. };
  439. ATTRIBUTE_GROUPS(net_class);
  440. /* Show a given an attribute in the statistics group */
  441. static ssize_t netstat_show(const struct device *d,
  442. struct device_attribute *attr, char *buf,
  443. unsigned long offset)
  444. {
  445. struct net_device *dev = to_net_dev(d);
  446. ssize_t ret = -EINVAL;
  447. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  448. offset % sizeof(u64) != 0);
  449. read_lock(&dev_base_lock);
  450. if (dev_isalive(dev)) {
  451. struct rtnl_link_stats64 temp;
  452. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  453. ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
  454. }
  455. read_unlock(&dev_base_lock);
  456. return ret;
  457. }
  458. /* generate a read-only statistics attribute */
  459. #define NETSTAT_ENTRY(name) \
  460. static ssize_t name##_show(struct device *d, \
  461. struct device_attribute *attr, char *buf) \
  462. { \
  463. return netstat_show(d, attr, buf, \
  464. offsetof(struct rtnl_link_stats64, name)); \
  465. } \
  466. static DEVICE_ATTR_RO(name)
  467. NETSTAT_ENTRY(rx_packets);
  468. NETSTAT_ENTRY(tx_packets);
  469. NETSTAT_ENTRY(rx_bytes);
  470. NETSTAT_ENTRY(tx_bytes);
  471. NETSTAT_ENTRY(rx_errors);
  472. NETSTAT_ENTRY(tx_errors);
  473. NETSTAT_ENTRY(rx_dropped);
  474. NETSTAT_ENTRY(tx_dropped);
  475. NETSTAT_ENTRY(multicast);
  476. NETSTAT_ENTRY(collisions);
  477. NETSTAT_ENTRY(rx_length_errors);
  478. NETSTAT_ENTRY(rx_over_errors);
  479. NETSTAT_ENTRY(rx_crc_errors);
  480. NETSTAT_ENTRY(rx_frame_errors);
  481. NETSTAT_ENTRY(rx_fifo_errors);
  482. NETSTAT_ENTRY(rx_missed_errors);
  483. NETSTAT_ENTRY(tx_aborted_errors);
  484. NETSTAT_ENTRY(tx_carrier_errors);
  485. NETSTAT_ENTRY(tx_fifo_errors);
  486. NETSTAT_ENTRY(tx_heartbeat_errors);
  487. NETSTAT_ENTRY(tx_window_errors);
  488. NETSTAT_ENTRY(rx_compressed);
  489. NETSTAT_ENTRY(tx_compressed);
  490. NETSTAT_ENTRY(rx_nohandler);
  491. static struct attribute *netstat_attrs[] = {
  492. &dev_attr_rx_packets.attr,
  493. &dev_attr_tx_packets.attr,
  494. &dev_attr_rx_bytes.attr,
  495. &dev_attr_tx_bytes.attr,
  496. &dev_attr_rx_errors.attr,
  497. &dev_attr_tx_errors.attr,
  498. &dev_attr_rx_dropped.attr,
  499. &dev_attr_tx_dropped.attr,
  500. &dev_attr_multicast.attr,
  501. &dev_attr_collisions.attr,
  502. &dev_attr_rx_length_errors.attr,
  503. &dev_attr_rx_over_errors.attr,
  504. &dev_attr_rx_crc_errors.attr,
  505. &dev_attr_rx_frame_errors.attr,
  506. &dev_attr_rx_fifo_errors.attr,
  507. &dev_attr_rx_missed_errors.attr,
  508. &dev_attr_tx_aborted_errors.attr,
  509. &dev_attr_tx_carrier_errors.attr,
  510. &dev_attr_tx_fifo_errors.attr,
  511. &dev_attr_tx_heartbeat_errors.attr,
  512. &dev_attr_tx_window_errors.attr,
  513. &dev_attr_rx_compressed.attr,
  514. &dev_attr_tx_compressed.attr,
  515. &dev_attr_rx_nohandler.attr,
  516. NULL
  517. };
  518. static struct attribute_group netstat_group = {
  519. .name = "statistics",
  520. .attrs = netstat_attrs,
  521. };
  522. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  523. static struct attribute *wireless_attrs[] = {
  524. NULL
  525. };
  526. static struct attribute_group wireless_group = {
  527. .name = "wireless",
  528. .attrs = wireless_attrs,
  529. };
  530. #endif
  531. #else /* CONFIG_SYSFS */
  532. #define net_class_groups NULL
  533. #endif /* CONFIG_SYSFS */
  534. #ifdef CONFIG_SYSFS
  535. #define to_rx_queue_attr(_attr) container_of(_attr, \
  536. struct rx_queue_attribute, attr)
  537. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  538. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  539. char *buf)
  540. {
  541. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  542. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  543. if (!attribute->show)
  544. return -EIO;
  545. return attribute->show(queue, attribute, buf);
  546. }
  547. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  548. const char *buf, size_t count)
  549. {
  550. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  551. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  552. if (!attribute->store)
  553. return -EIO;
  554. return attribute->store(queue, attribute, buf, count);
  555. }
  556. static const struct sysfs_ops rx_queue_sysfs_ops = {
  557. .show = rx_queue_attr_show,
  558. .store = rx_queue_attr_store,
  559. };
  560. #ifdef CONFIG_RPS
  561. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  562. struct rx_queue_attribute *attribute, char *buf)
  563. {
  564. struct rps_map *map;
  565. cpumask_var_t mask;
  566. int i, len;
  567. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  568. return -ENOMEM;
  569. rcu_read_lock();
  570. map = rcu_dereference(queue->rps_map);
  571. if (map)
  572. for (i = 0; i < map->len; i++)
  573. cpumask_set_cpu(map->cpus[i], mask);
  574. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  575. rcu_read_unlock();
  576. free_cpumask_var(mask);
  577. return len < PAGE_SIZE ? len : -EINVAL;
  578. }
  579. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  580. struct rx_queue_attribute *attribute,
  581. const char *buf, size_t len)
  582. {
  583. struct rps_map *old_map, *map;
  584. cpumask_var_t mask;
  585. int err, cpu, i;
  586. static DEFINE_MUTEX(rps_map_mutex);
  587. if (!capable(CAP_NET_ADMIN))
  588. return -EPERM;
  589. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  590. return -ENOMEM;
  591. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  592. if (err) {
  593. free_cpumask_var(mask);
  594. return err;
  595. }
  596. map = kzalloc(max_t(unsigned int,
  597. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  598. GFP_KERNEL);
  599. if (!map) {
  600. free_cpumask_var(mask);
  601. return -ENOMEM;
  602. }
  603. i = 0;
  604. for_each_cpu_and(cpu, mask, cpu_online_mask)
  605. map->cpus[i++] = cpu;
  606. if (i)
  607. map->len = i;
  608. else {
  609. kfree(map);
  610. map = NULL;
  611. }
  612. mutex_lock(&rps_map_mutex);
  613. old_map = rcu_dereference_protected(queue->rps_map,
  614. mutex_is_locked(&rps_map_mutex));
  615. rcu_assign_pointer(queue->rps_map, map);
  616. if (map)
  617. static_key_slow_inc(&rps_needed);
  618. if (old_map)
  619. static_key_slow_dec(&rps_needed);
  620. mutex_unlock(&rps_map_mutex);
  621. if (old_map)
  622. kfree_rcu(old_map, rcu);
  623. free_cpumask_var(mask);
  624. return len;
  625. }
  626. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  627. struct rx_queue_attribute *attr,
  628. char *buf)
  629. {
  630. struct rps_dev_flow_table *flow_table;
  631. unsigned long val = 0;
  632. rcu_read_lock();
  633. flow_table = rcu_dereference(queue->rps_flow_table);
  634. if (flow_table)
  635. val = (unsigned long)flow_table->mask + 1;
  636. rcu_read_unlock();
  637. return sprintf(buf, "%lu\n", val);
  638. }
  639. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  640. {
  641. struct rps_dev_flow_table *table = container_of(rcu,
  642. struct rps_dev_flow_table, rcu);
  643. vfree(table);
  644. }
  645. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  646. struct rx_queue_attribute *attr,
  647. const char *buf, size_t len)
  648. {
  649. unsigned long mask, count;
  650. struct rps_dev_flow_table *table, *old_table;
  651. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  652. int rc;
  653. if (!capable(CAP_NET_ADMIN))
  654. return -EPERM;
  655. rc = kstrtoul(buf, 0, &count);
  656. if (rc < 0)
  657. return rc;
  658. if (count) {
  659. mask = count - 1;
  660. /* mask = roundup_pow_of_two(count) - 1;
  661. * without overflows...
  662. */
  663. while ((mask | (mask >> 1)) != mask)
  664. mask |= (mask >> 1);
  665. /* On 64 bit arches, must check mask fits in table->mask (u32),
  666. * and on 32bit arches, must check
  667. * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
  668. */
  669. #if BITS_PER_LONG > 32
  670. if (mask > (unsigned long)(u32)mask)
  671. return -EINVAL;
  672. #else
  673. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  674. / sizeof(struct rps_dev_flow)) {
  675. /* Enforce a limit to prevent overflow */
  676. return -EINVAL;
  677. }
  678. #endif
  679. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  680. if (!table)
  681. return -ENOMEM;
  682. table->mask = mask;
  683. for (count = 0; count <= mask; count++)
  684. table->flows[count].cpu = RPS_NO_CPU;
  685. } else
  686. table = NULL;
  687. spin_lock(&rps_dev_flow_lock);
  688. old_table = rcu_dereference_protected(queue->rps_flow_table,
  689. lockdep_is_held(&rps_dev_flow_lock));
  690. rcu_assign_pointer(queue->rps_flow_table, table);
  691. spin_unlock(&rps_dev_flow_lock);
  692. if (old_table)
  693. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  694. return len;
  695. }
  696. static struct rx_queue_attribute rps_cpus_attribute =
  697. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  698. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  699. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  700. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  701. #endif /* CONFIG_RPS */
  702. static struct attribute *rx_queue_default_attrs[] = {
  703. #ifdef CONFIG_RPS
  704. &rps_cpus_attribute.attr,
  705. &rps_dev_flow_table_cnt_attribute.attr,
  706. #endif
  707. NULL
  708. };
  709. static void rx_queue_release(struct kobject *kobj)
  710. {
  711. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  712. #ifdef CONFIG_RPS
  713. struct rps_map *map;
  714. struct rps_dev_flow_table *flow_table;
  715. map = rcu_dereference_protected(queue->rps_map, 1);
  716. if (map) {
  717. RCU_INIT_POINTER(queue->rps_map, NULL);
  718. kfree_rcu(map, rcu);
  719. }
  720. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  721. if (flow_table) {
  722. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  723. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  724. }
  725. #endif
  726. memset(kobj, 0, sizeof(*kobj));
  727. dev_put(queue->dev);
  728. }
  729. static const void *rx_queue_namespace(struct kobject *kobj)
  730. {
  731. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  732. struct device *dev = &queue->dev->dev;
  733. const void *ns = NULL;
  734. if (dev->class && dev->class->ns_type)
  735. ns = dev->class->namespace(dev);
  736. return ns;
  737. }
  738. static struct kobj_type rx_queue_ktype = {
  739. .sysfs_ops = &rx_queue_sysfs_ops,
  740. .release = rx_queue_release,
  741. .default_attrs = rx_queue_default_attrs,
  742. .namespace = rx_queue_namespace
  743. };
  744. static int rx_queue_add_kobject(struct net_device *dev, int index)
  745. {
  746. struct netdev_rx_queue *queue = dev->_rx + index;
  747. struct kobject *kobj = &queue->kobj;
  748. int error = 0;
  749. kobj->kset = dev->queues_kset;
  750. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  751. "rx-%u", index);
  752. if (error)
  753. goto exit;
  754. if (dev->sysfs_rx_queue_group) {
  755. error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
  756. if (error)
  757. goto exit;
  758. }
  759. kobject_uevent(kobj, KOBJ_ADD);
  760. dev_hold(queue->dev);
  761. return error;
  762. exit:
  763. kobject_put(kobj);
  764. return error;
  765. }
  766. #endif /* CONFIG_SYSFS */
  767. int
  768. net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  769. {
  770. #ifdef CONFIG_SYSFS
  771. int i;
  772. int error = 0;
  773. #ifndef CONFIG_RPS
  774. if (!dev->sysfs_rx_queue_group)
  775. return 0;
  776. #endif
  777. for (i = old_num; i < new_num; i++) {
  778. error = rx_queue_add_kobject(dev, i);
  779. if (error) {
  780. new_num = old_num;
  781. break;
  782. }
  783. }
  784. while (--i >= new_num) {
  785. if (dev->sysfs_rx_queue_group)
  786. sysfs_remove_group(&dev->_rx[i].kobj,
  787. dev->sysfs_rx_queue_group);
  788. kobject_put(&dev->_rx[i].kobj);
  789. }
  790. return error;
  791. #else
  792. return 0;
  793. #endif
  794. }
  795. #ifdef CONFIG_SYSFS
  796. /*
  797. * netdev_queue sysfs structures and functions.
  798. */
  799. struct netdev_queue_attribute {
  800. struct attribute attr;
  801. ssize_t (*show)(struct netdev_queue *queue,
  802. struct netdev_queue_attribute *attr, char *buf);
  803. ssize_t (*store)(struct netdev_queue *queue,
  804. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  805. };
  806. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  807. struct netdev_queue_attribute, attr)
  808. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  809. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  810. struct attribute *attr, char *buf)
  811. {
  812. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  813. struct netdev_queue *queue = to_netdev_queue(kobj);
  814. if (!attribute->show)
  815. return -EIO;
  816. return attribute->show(queue, attribute, buf);
  817. }
  818. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  819. struct attribute *attr,
  820. const char *buf, size_t count)
  821. {
  822. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  823. struct netdev_queue *queue = to_netdev_queue(kobj);
  824. if (!attribute->store)
  825. return -EIO;
  826. return attribute->store(queue, attribute, buf, count);
  827. }
  828. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  829. .show = netdev_queue_attr_show,
  830. .store = netdev_queue_attr_store,
  831. };
  832. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  833. struct netdev_queue_attribute *attribute,
  834. char *buf)
  835. {
  836. unsigned long trans_timeout;
  837. spin_lock_irq(&queue->_xmit_lock);
  838. trans_timeout = queue->trans_timeout;
  839. spin_unlock_irq(&queue->_xmit_lock);
  840. return sprintf(buf, "%lu", trans_timeout);
  841. }
  842. #ifdef CONFIG_XPS
  843. static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  844. {
  845. struct net_device *dev = queue->dev;
  846. unsigned int i;
  847. i = queue - dev->_tx;
  848. BUG_ON(i >= dev->num_tx_queues);
  849. return i;
  850. }
  851. static ssize_t show_tx_maxrate(struct netdev_queue *queue,
  852. struct netdev_queue_attribute *attribute,
  853. char *buf)
  854. {
  855. return sprintf(buf, "%lu\n", queue->tx_maxrate);
  856. }
  857. static ssize_t set_tx_maxrate(struct netdev_queue *queue,
  858. struct netdev_queue_attribute *attribute,
  859. const char *buf, size_t len)
  860. {
  861. struct net_device *dev = queue->dev;
  862. int err, index = get_netdev_queue_index(queue);
  863. u32 rate = 0;
  864. err = kstrtou32(buf, 10, &rate);
  865. if (err < 0)
  866. return err;
  867. if (!rtnl_trylock())
  868. return restart_syscall();
  869. err = -EOPNOTSUPP;
  870. if (dev->netdev_ops->ndo_set_tx_maxrate)
  871. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  872. rtnl_unlock();
  873. if (!err) {
  874. queue->tx_maxrate = rate;
  875. return len;
  876. }
  877. return err;
  878. }
  879. static struct netdev_queue_attribute queue_tx_maxrate =
  880. __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
  881. show_tx_maxrate, set_tx_maxrate);
  882. #endif
  883. static struct netdev_queue_attribute queue_trans_timeout =
  884. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  885. #ifdef CONFIG_BQL
  886. /*
  887. * Byte queue limits sysfs structures and functions.
  888. */
  889. static ssize_t bql_show(char *buf, unsigned int value)
  890. {
  891. return sprintf(buf, "%u\n", value);
  892. }
  893. static ssize_t bql_set(const char *buf, const size_t count,
  894. unsigned int *pvalue)
  895. {
  896. unsigned int value;
  897. int err;
  898. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  899. value = DQL_MAX_LIMIT;
  900. else {
  901. err = kstrtouint(buf, 10, &value);
  902. if (err < 0)
  903. return err;
  904. if (value > DQL_MAX_LIMIT)
  905. return -EINVAL;
  906. }
  907. *pvalue = value;
  908. return count;
  909. }
  910. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  911. struct netdev_queue_attribute *attr,
  912. char *buf)
  913. {
  914. struct dql *dql = &queue->dql;
  915. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  916. }
  917. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  918. struct netdev_queue_attribute *attribute,
  919. const char *buf, size_t len)
  920. {
  921. struct dql *dql = &queue->dql;
  922. unsigned int value;
  923. int err;
  924. err = kstrtouint(buf, 10, &value);
  925. if (err < 0)
  926. return err;
  927. dql->slack_hold_time = msecs_to_jiffies(value);
  928. return len;
  929. }
  930. static struct netdev_queue_attribute bql_hold_time_attribute =
  931. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  932. bql_set_hold_time);
  933. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  934. struct netdev_queue_attribute *attr,
  935. char *buf)
  936. {
  937. struct dql *dql = &queue->dql;
  938. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  939. }
  940. static struct netdev_queue_attribute bql_inflight_attribute =
  941. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  942. #define BQL_ATTR(NAME, FIELD) \
  943. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  944. struct netdev_queue_attribute *attr, \
  945. char *buf) \
  946. { \
  947. return bql_show(buf, queue->dql.FIELD); \
  948. } \
  949. \
  950. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  951. struct netdev_queue_attribute *attr, \
  952. const char *buf, size_t len) \
  953. { \
  954. return bql_set(buf, len, &queue->dql.FIELD); \
  955. } \
  956. \
  957. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  958. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  959. bql_set_ ## NAME);
  960. BQL_ATTR(limit, limit)
  961. BQL_ATTR(limit_max, max_limit)
  962. BQL_ATTR(limit_min, min_limit)
  963. static struct attribute *dql_attrs[] = {
  964. &bql_limit_attribute.attr,
  965. &bql_limit_max_attribute.attr,
  966. &bql_limit_min_attribute.attr,
  967. &bql_hold_time_attribute.attr,
  968. &bql_inflight_attribute.attr,
  969. NULL
  970. };
  971. static struct attribute_group dql_group = {
  972. .name = "byte_queue_limits",
  973. .attrs = dql_attrs,
  974. };
  975. #endif /* CONFIG_BQL */
  976. #ifdef CONFIG_XPS
  977. static ssize_t show_xps_map(struct netdev_queue *queue,
  978. struct netdev_queue_attribute *attribute, char *buf)
  979. {
  980. struct net_device *dev = queue->dev;
  981. struct xps_dev_maps *dev_maps;
  982. cpumask_var_t mask;
  983. unsigned long index;
  984. int i, len;
  985. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  986. return -ENOMEM;
  987. index = get_netdev_queue_index(queue);
  988. rcu_read_lock();
  989. dev_maps = rcu_dereference(dev->xps_maps);
  990. if (dev_maps) {
  991. for_each_possible_cpu(i) {
  992. struct xps_map *map =
  993. rcu_dereference(dev_maps->cpu_map[i]);
  994. if (map) {
  995. int j;
  996. for (j = 0; j < map->len; j++) {
  997. if (map->queues[j] == index) {
  998. cpumask_set_cpu(i, mask);
  999. break;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. rcu_read_unlock();
  1006. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  1007. free_cpumask_var(mask);
  1008. return len < PAGE_SIZE ? len : -EINVAL;
  1009. }
  1010. static ssize_t store_xps_map(struct netdev_queue *queue,
  1011. struct netdev_queue_attribute *attribute,
  1012. const char *buf, size_t len)
  1013. {
  1014. struct net_device *dev = queue->dev;
  1015. unsigned long index;
  1016. cpumask_var_t mask;
  1017. int err;
  1018. if (!capable(CAP_NET_ADMIN))
  1019. return -EPERM;
  1020. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1021. return -ENOMEM;
  1022. index = get_netdev_queue_index(queue);
  1023. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1024. if (err) {
  1025. free_cpumask_var(mask);
  1026. return err;
  1027. }
  1028. err = netif_set_xps_queue(dev, mask, index);
  1029. free_cpumask_var(mask);
  1030. return err ? : len;
  1031. }
  1032. static struct netdev_queue_attribute xps_cpus_attribute =
  1033. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  1034. #endif /* CONFIG_XPS */
  1035. static struct attribute *netdev_queue_default_attrs[] = {
  1036. &queue_trans_timeout.attr,
  1037. #ifdef CONFIG_XPS
  1038. &xps_cpus_attribute.attr,
  1039. &queue_tx_maxrate.attr,
  1040. #endif
  1041. NULL
  1042. };
  1043. static void netdev_queue_release(struct kobject *kobj)
  1044. {
  1045. struct netdev_queue *queue = to_netdev_queue(kobj);
  1046. memset(kobj, 0, sizeof(*kobj));
  1047. dev_put(queue->dev);
  1048. }
  1049. static const void *netdev_queue_namespace(struct kobject *kobj)
  1050. {
  1051. struct netdev_queue *queue = to_netdev_queue(kobj);
  1052. struct device *dev = &queue->dev->dev;
  1053. const void *ns = NULL;
  1054. if (dev->class && dev->class->ns_type)
  1055. ns = dev->class->namespace(dev);
  1056. return ns;
  1057. }
  1058. static struct kobj_type netdev_queue_ktype = {
  1059. .sysfs_ops = &netdev_queue_sysfs_ops,
  1060. .release = netdev_queue_release,
  1061. .default_attrs = netdev_queue_default_attrs,
  1062. .namespace = netdev_queue_namespace,
  1063. };
  1064. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1065. {
  1066. struct netdev_queue *queue = dev->_tx + index;
  1067. struct kobject *kobj = &queue->kobj;
  1068. int error = 0;
  1069. kobj->kset = dev->queues_kset;
  1070. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1071. "tx-%u", index);
  1072. if (error)
  1073. goto exit;
  1074. #ifdef CONFIG_BQL
  1075. error = sysfs_create_group(kobj, &dql_group);
  1076. if (error)
  1077. goto exit;
  1078. #endif
  1079. kobject_uevent(kobj, KOBJ_ADD);
  1080. dev_hold(queue->dev);
  1081. return 0;
  1082. exit:
  1083. kobject_put(kobj);
  1084. return error;
  1085. }
  1086. #endif /* CONFIG_SYSFS */
  1087. int
  1088. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1089. {
  1090. #ifdef CONFIG_SYSFS
  1091. int i;
  1092. int error = 0;
  1093. for (i = old_num; i < new_num; i++) {
  1094. error = netdev_queue_add_kobject(dev, i);
  1095. if (error) {
  1096. new_num = old_num;
  1097. break;
  1098. }
  1099. }
  1100. while (--i >= new_num) {
  1101. struct netdev_queue *queue = dev->_tx + i;
  1102. #ifdef CONFIG_BQL
  1103. sysfs_remove_group(&queue->kobj, &dql_group);
  1104. #endif
  1105. kobject_put(&queue->kobj);
  1106. }
  1107. return error;
  1108. #else
  1109. return 0;
  1110. #endif /* CONFIG_SYSFS */
  1111. }
  1112. static int register_queue_kobjects(struct net_device *dev)
  1113. {
  1114. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1115. #ifdef CONFIG_SYSFS
  1116. dev->queues_kset = kset_create_and_add("queues",
  1117. NULL, &dev->dev.kobj);
  1118. if (!dev->queues_kset)
  1119. return -ENOMEM;
  1120. real_rx = dev->real_num_rx_queues;
  1121. #endif
  1122. real_tx = dev->real_num_tx_queues;
  1123. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1124. if (error)
  1125. goto error;
  1126. rxq = real_rx;
  1127. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1128. if (error)
  1129. goto error;
  1130. txq = real_tx;
  1131. return 0;
  1132. error:
  1133. netdev_queue_update_kobjects(dev, txq, 0);
  1134. net_rx_queue_update_kobjects(dev, rxq, 0);
  1135. return error;
  1136. }
  1137. static void remove_queue_kobjects(struct net_device *dev)
  1138. {
  1139. int real_rx = 0, real_tx = 0;
  1140. #ifdef CONFIG_SYSFS
  1141. real_rx = dev->real_num_rx_queues;
  1142. #endif
  1143. real_tx = dev->real_num_tx_queues;
  1144. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1145. netdev_queue_update_kobjects(dev, real_tx, 0);
  1146. #ifdef CONFIG_SYSFS
  1147. kset_unregister(dev->queues_kset);
  1148. #endif
  1149. }
  1150. static bool net_current_may_mount(void)
  1151. {
  1152. struct net *net = current->nsproxy->net_ns;
  1153. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1154. }
  1155. static void *net_grab_current_ns(void)
  1156. {
  1157. struct net *ns = current->nsproxy->net_ns;
  1158. #ifdef CONFIG_NET_NS
  1159. if (ns)
  1160. atomic_inc(&ns->passive);
  1161. #endif
  1162. return ns;
  1163. }
  1164. static const void *net_initial_ns(void)
  1165. {
  1166. return &init_net;
  1167. }
  1168. static const void *net_netlink_ns(struct sock *sk)
  1169. {
  1170. return sock_net(sk);
  1171. }
  1172. struct kobj_ns_type_operations net_ns_type_operations = {
  1173. .type = KOBJ_NS_TYPE_NET,
  1174. .current_may_mount = net_current_may_mount,
  1175. .grab_current_ns = net_grab_current_ns,
  1176. .netlink_ns = net_netlink_ns,
  1177. .initial_ns = net_initial_ns,
  1178. .drop_ns = net_drop_ns,
  1179. };
  1180. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1181. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1182. {
  1183. struct net_device *dev = to_net_dev(d);
  1184. int retval;
  1185. /* pass interface to uevent. */
  1186. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1187. if (retval)
  1188. goto exit;
  1189. /* pass ifindex to uevent.
  1190. * ifindex is useful as it won't change (interface name may change)
  1191. * and is what RtNetlink uses natively. */
  1192. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1193. exit:
  1194. return retval;
  1195. }
  1196. /*
  1197. * netdev_release -- destroy and free a dead device.
  1198. * Called when last reference to device kobject is gone.
  1199. */
  1200. static void netdev_release(struct device *d)
  1201. {
  1202. struct net_device *dev = to_net_dev(d);
  1203. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1204. kfree(dev->ifalias);
  1205. netdev_freemem(dev);
  1206. }
  1207. static const void *net_namespace(struct device *d)
  1208. {
  1209. struct net_device *dev = to_net_dev(d);
  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. }