net-sysfs.c 38 KB

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