net-sysfs.c 37 KB

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