net-sysfs.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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_up_count) +
  249. atomic_read(&netdev->carrier_down_count));
  250. }
  251. static DEVICE_ATTR_RO(carrier_changes);
  252. static ssize_t carrier_up_count_show(struct device *dev,
  253. struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct net_device *netdev = to_net_dev(dev);
  257. return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
  258. }
  259. static DEVICE_ATTR_RO(carrier_up_count);
  260. static ssize_t carrier_down_count_show(struct device *dev,
  261. struct device_attribute *attr,
  262. char *buf)
  263. {
  264. struct net_device *netdev = to_net_dev(dev);
  265. return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
  266. }
  267. static DEVICE_ATTR_RO(carrier_down_count);
  268. /* read-write attributes */
  269. static int change_mtu(struct net_device *dev, unsigned long new_mtu)
  270. {
  271. return dev_set_mtu(dev, (int)new_mtu);
  272. }
  273. static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
  274. const char *buf, size_t len)
  275. {
  276. return netdev_store(dev, attr, buf, len, change_mtu);
  277. }
  278. NETDEVICE_SHOW_RW(mtu, fmt_dec);
  279. static int change_flags(struct net_device *dev, unsigned long new_flags)
  280. {
  281. return dev_change_flags(dev, (unsigned int)new_flags);
  282. }
  283. static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
  284. const char *buf, size_t len)
  285. {
  286. return netdev_store(dev, attr, buf, len, change_flags);
  287. }
  288. NETDEVICE_SHOW_RW(flags, fmt_hex);
  289. static ssize_t tx_queue_len_store(struct device *dev,
  290. struct device_attribute *attr,
  291. const char *buf, size_t len)
  292. {
  293. if (!capable(CAP_NET_ADMIN))
  294. return -EPERM;
  295. return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
  296. }
  297. NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
  298. static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
  299. {
  300. dev->gro_flush_timeout = val;
  301. return 0;
  302. }
  303. static ssize_t gro_flush_timeout_store(struct device *dev,
  304. struct device_attribute *attr,
  305. const char *buf, size_t len)
  306. {
  307. if (!capable(CAP_NET_ADMIN))
  308. return -EPERM;
  309. return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
  310. }
  311. NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
  312. static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
  313. const char *buf, size_t len)
  314. {
  315. struct net_device *netdev = to_net_dev(dev);
  316. struct net *net = dev_net(netdev);
  317. size_t count = len;
  318. ssize_t ret = 0;
  319. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  320. return -EPERM;
  321. /* ignore trailing newline */
  322. if (len > 0 && buf[len - 1] == '\n')
  323. --count;
  324. if (!rtnl_trylock())
  325. return restart_syscall();
  326. if (dev_isalive(netdev)) {
  327. ret = dev_set_alias(netdev, buf, count);
  328. if (ret < 0)
  329. goto err;
  330. ret = len;
  331. netdev_state_change(netdev);
  332. }
  333. err:
  334. rtnl_unlock();
  335. return ret;
  336. }
  337. static ssize_t ifalias_show(struct device *dev,
  338. struct device_attribute *attr, char *buf)
  339. {
  340. const struct net_device *netdev = to_net_dev(dev);
  341. char tmp[IFALIASZ];
  342. ssize_t ret = 0;
  343. ret = dev_get_alias(netdev, tmp, sizeof(tmp));
  344. if (ret > 0)
  345. ret = sprintf(buf, "%s\n", tmp);
  346. return ret;
  347. }
  348. static DEVICE_ATTR_RW(ifalias);
  349. static int change_group(struct net_device *dev, unsigned long new_group)
  350. {
  351. dev_set_group(dev, (int)new_group);
  352. return 0;
  353. }
  354. static ssize_t group_store(struct device *dev, struct device_attribute *attr,
  355. const char *buf, size_t len)
  356. {
  357. return netdev_store(dev, attr, buf, len, change_group);
  358. }
  359. NETDEVICE_SHOW(group, fmt_dec);
  360. static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
  361. static int change_proto_down(struct net_device *dev, unsigned long proto_down)
  362. {
  363. return dev_change_proto_down(dev, (bool)proto_down);
  364. }
  365. static ssize_t proto_down_store(struct device *dev,
  366. struct device_attribute *attr,
  367. const char *buf, size_t len)
  368. {
  369. return netdev_store(dev, attr, buf, len, change_proto_down);
  370. }
  371. NETDEVICE_SHOW_RW(proto_down, fmt_dec);
  372. static ssize_t phys_port_id_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct net_device *netdev = to_net_dev(dev);
  376. ssize_t ret = -EINVAL;
  377. if (!rtnl_trylock())
  378. return restart_syscall();
  379. if (dev_isalive(netdev)) {
  380. struct netdev_phys_item_id ppid;
  381. ret = dev_get_phys_port_id(netdev, &ppid);
  382. if (!ret)
  383. ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
  384. }
  385. rtnl_unlock();
  386. return ret;
  387. }
  388. static DEVICE_ATTR_RO(phys_port_id);
  389. static ssize_t phys_port_name_show(struct device *dev,
  390. struct device_attribute *attr, char *buf)
  391. {
  392. struct net_device *netdev = to_net_dev(dev);
  393. ssize_t ret = -EINVAL;
  394. if (!rtnl_trylock())
  395. return restart_syscall();
  396. if (dev_isalive(netdev)) {
  397. char name[IFNAMSIZ];
  398. ret = dev_get_phys_port_name(netdev, name, sizeof(name));
  399. if (!ret)
  400. ret = sprintf(buf, "%s\n", name);
  401. }
  402. rtnl_unlock();
  403. return ret;
  404. }
  405. static DEVICE_ATTR_RO(phys_port_name);
  406. static ssize_t phys_switch_id_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct net_device *netdev = to_net_dev(dev);
  410. ssize_t ret = -EINVAL;
  411. if (!rtnl_trylock())
  412. return restart_syscall();
  413. if (dev_isalive(netdev)) {
  414. struct switchdev_attr attr = {
  415. .orig_dev = netdev,
  416. .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  417. .flags = SWITCHDEV_F_NO_RECURSE,
  418. };
  419. ret = switchdev_port_attr_get(netdev, &attr);
  420. if (!ret)
  421. ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
  422. attr.u.ppid.id);
  423. }
  424. rtnl_unlock();
  425. return ret;
  426. }
  427. static DEVICE_ATTR_RO(phys_switch_id);
  428. static struct attribute *net_class_attrs[] __ro_after_init = {
  429. &dev_attr_netdev_group.attr,
  430. &dev_attr_type.attr,
  431. &dev_attr_dev_id.attr,
  432. &dev_attr_dev_port.attr,
  433. &dev_attr_iflink.attr,
  434. &dev_attr_ifindex.attr,
  435. &dev_attr_name_assign_type.attr,
  436. &dev_attr_addr_assign_type.attr,
  437. &dev_attr_addr_len.attr,
  438. &dev_attr_link_mode.attr,
  439. &dev_attr_address.attr,
  440. &dev_attr_broadcast.attr,
  441. &dev_attr_speed.attr,
  442. &dev_attr_duplex.attr,
  443. &dev_attr_dormant.attr,
  444. &dev_attr_operstate.attr,
  445. &dev_attr_carrier_changes.attr,
  446. &dev_attr_ifalias.attr,
  447. &dev_attr_carrier.attr,
  448. &dev_attr_mtu.attr,
  449. &dev_attr_flags.attr,
  450. &dev_attr_tx_queue_len.attr,
  451. &dev_attr_gro_flush_timeout.attr,
  452. &dev_attr_phys_port_id.attr,
  453. &dev_attr_phys_port_name.attr,
  454. &dev_attr_phys_switch_id.attr,
  455. &dev_attr_proto_down.attr,
  456. &dev_attr_carrier_up_count.attr,
  457. &dev_attr_carrier_down_count.attr,
  458. NULL,
  459. };
  460. ATTRIBUTE_GROUPS(net_class);
  461. /* Show a given an attribute in the statistics group */
  462. static ssize_t netstat_show(const struct device *d,
  463. struct device_attribute *attr, char *buf,
  464. unsigned long offset)
  465. {
  466. struct net_device *dev = to_net_dev(d);
  467. ssize_t ret = -EINVAL;
  468. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  469. offset % sizeof(u64) != 0);
  470. read_lock(&dev_base_lock);
  471. if (dev_isalive(dev)) {
  472. struct rtnl_link_stats64 temp;
  473. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  474. ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
  475. }
  476. read_unlock(&dev_base_lock);
  477. return ret;
  478. }
  479. /* generate a read-only statistics attribute */
  480. #define NETSTAT_ENTRY(name) \
  481. static ssize_t name##_show(struct device *d, \
  482. struct device_attribute *attr, char *buf) \
  483. { \
  484. return netstat_show(d, attr, buf, \
  485. offsetof(struct rtnl_link_stats64, name)); \
  486. } \
  487. static DEVICE_ATTR_RO(name)
  488. NETSTAT_ENTRY(rx_packets);
  489. NETSTAT_ENTRY(tx_packets);
  490. NETSTAT_ENTRY(rx_bytes);
  491. NETSTAT_ENTRY(tx_bytes);
  492. NETSTAT_ENTRY(rx_errors);
  493. NETSTAT_ENTRY(tx_errors);
  494. NETSTAT_ENTRY(rx_dropped);
  495. NETSTAT_ENTRY(tx_dropped);
  496. NETSTAT_ENTRY(multicast);
  497. NETSTAT_ENTRY(collisions);
  498. NETSTAT_ENTRY(rx_length_errors);
  499. NETSTAT_ENTRY(rx_over_errors);
  500. NETSTAT_ENTRY(rx_crc_errors);
  501. NETSTAT_ENTRY(rx_frame_errors);
  502. NETSTAT_ENTRY(rx_fifo_errors);
  503. NETSTAT_ENTRY(rx_missed_errors);
  504. NETSTAT_ENTRY(tx_aborted_errors);
  505. NETSTAT_ENTRY(tx_carrier_errors);
  506. NETSTAT_ENTRY(tx_fifo_errors);
  507. NETSTAT_ENTRY(tx_heartbeat_errors);
  508. NETSTAT_ENTRY(tx_window_errors);
  509. NETSTAT_ENTRY(rx_compressed);
  510. NETSTAT_ENTRY(tx_compressed);
  511. NETSTAT_ENTRY(rx_nohandler);
  512. static struct attribute *netstat_attrs[] __ro_after_init = {
  513. &dev_attr_rx_packets.attr,
  514. &dev_attr_tx_packets.attr,
  515. &dev_attr_rx_bytes.attr,
  516. &dev_attr_tx_bytes.attr,
  517. &dev_attr_rx_errors.attr,
  518. &dev_attr_tx_errors.attr,
  519. &dev_attr_rx_dropped.attr,
  520. &dev_attr_tx_dropped.attr,
  521. &dev_attr_multicast.attr,
  522. &dev_attr_collisions.attr,
  523. &dev_attr_rx_length_errors.attr,
  524. &dev_attr_rx_over_errors.attr,
  525. &dev_attr_rx_crc_errors.attr,
  526. &dev_attr_rx_frame_errors.attr,
  527. &dev_attr_rx_fifo_errors.attr,
  528. &dev_attr_rx_missed_errors.attr,
  529. &dev_attr_tx_aborted_errors.attr,
  530. &dev_attr_tx_carrier_errors.attr,
  531. &dev_attr_tx_fifo_errors.attr,
  532. &dev_attr_tx_heartbeat_errors.attr,
  533. &dev_attr_tx_window_errors.attr,
  534. &dev_attr_rx_compressed.attr,
  535. &dev_attr_tx_compressed.attr,
  536. &dev_attr_rx_nohandler.attr,
  537. NULL
  538. };
  539. static const struct attribute_group netstat_group = {
  540. .name = "statistics",
  541. .attrs = netstat_attrs,
  542. };
  543. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  544. static struct attribute *wireless_attrs[] = {
  545. NULL
  546. };
  547. static const struct attribute_group wireless_group = {
  548. .name = "wireless",
  549. .attrs = wireless_attrs,
  550. };
  551. #endif
  552. #else /* CONFIG_SYSFS */
  553. #define net_class_groups NULL
  554. #endif /* CONFIG_SYSFS */
  555. #ifdef CONFIG_SYSFS
  556. #define to_rx_queue_attr(_attr) \
  557. container_of(_attr, struct rx_queue_attribute, attr)
  558. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  559. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  560. char *buf)
  561. {
  562. const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  563. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  564. if (!attribute->show)
  565. return -EIO;
  566. return attribute->show(queue, buf);
  567. }
  568. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  569. const char *buf, size_t count)
  570. {
  571. const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  572. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  573. if (!attribute->store)
  574. return -EIO;
  575. return attribute->store(queue, buf, count);
  576. }
  577. static const struct sysfs_ops rx_queue_sysfs_ops = {
  578. .show = rx_queue_attr_show,
  579. .store = rx_queue_attr_store,
  580. };
  581. #ifdef CONFIG_RPS
  582. static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
  583. {
  584. struct rps_map *map;
  585. cpumask_var_t mask;
  586. int i, len;
  587. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  588. return -ENOMEM;
  589. rcu_read_lock();
  590. map = rcu_dereference(queue->rps_map);
  591. if (map)
  592. for (i = 0; i < map->len; i++)
  593. cpumask_set_cpu(map->cpus[i], mask);
  594. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  595. rcu_read_unlock();
  596. free_cpumask_var(mask);
  597. return len < PAGE_SIZE ? len : -EINVAL;
  598. }
  599. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  600. const char *buf, size_t len)
  601. {
  602. struct rps_map *old_map, *map;
  603. cpumask_var_t mask;
  604. int err, cpu, i;
  605. static DEFINE_MUTEX(rps_map_mutex);
  606. if (!capable(CAP_NET_ADMIN))
  607. return -EPERM;
  608. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  609. return -ENOMEM;
  610. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  611. if (err) {
  612. free_cpumask_var(mask);
  613. return err;
  614. }
  615. map = kzalloc(max_t(unsigned int,
  616. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  617. GFP_KERNEL);
  618. if (!map) {
  619. free_cpumask_var(mask);
  620. return -ENOMEM;
  621. }
  622. i = 0;
  623. for_each_cpu_and(cpu, mask, cpu_online_mask)
  624. map->cpus[i++] = cpu;
  625. if (i) {
  626. map->len = i;
  627. } else {
  628. kfree(map);
  629. map = NULL;
  630. }
  631. mutex_lock(&rps_map_mutex);
  632. old_map = rcu_dereference_protected(queue->rps_map,
  633. mutex_is_locked(&rps_map_mutex));
  634. rcu_assign_pointer(queue->rps_map, map);
  635. if (map)
  636. static_key_slow_inc(&rps_needed);
  637. if (old_map)
  638. static_key_slow_dec(&rps_needed);
  639. mutex_unlock(&rps_map_mutex);
  640. if (old_map)
  641. kfree_rcu(old_map, rcu);
  642. free_cpumask_var(mask);
  643. return len;
  644. }
  645. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  646. char *buf)
  647. {
  648. struct rps_dev_flow_table *flow_table;
  649. unsigned long val = 0;
  650. rcu_read_lock();
  651. flow_table = rcu_dereference(queue->rps_flow_table);
  652. if (flow_table)
  653. val = (unsigned long)flow_table->mask + 1;
  654. rcu_read_unlock();
  655. return sprintf(buf, "%lu\n", val);
  656. }
  657. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  658. {
  659. struct rps_dev_flow_table *table = container_of(rcu,
  660. struct rps_dev_flow_table, rcu);
  661. vfree(table);
  662. }
  663. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  664. const char *buf, size_t len)
  665. {
  666. unsigned long mask, count;
  667. struct rps_dev_flow_table *table, *old_table;
  668. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  669. int rc;
  670. if (!capable(CAP_NET_ADMIN))
  671. return -EPERM;
  672. rc = kstrtoul(buf, 0, &count);
  673. if (rc < 0)
  674. return rc;
  675. if (count) {
  676. mask = count - 1;
  677. /* mask = roundup_pow_of_two(count) - 1;
  678. * without overflows...
  679. */
  680. while ((mask | (mask >> 1)) != mask)
  681. mask |= (mask >> 1);
  682. /* On 64 bit arches, must check mask fits in table->mask (u32),
  683. * and on 32bit arches, must check
  684. * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
  685. */
  686. #if BITS_PER_LONG > 32
  687. if (mask > (unsigned long)(u32)mask)
  688. return -EINVAL;
  689. #else
  690. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  691. / sizeof(struct rps_dev_flow)) {
  692. /* Enforce a limit to prevent overflow */
  693. return -EINVAL;
  694. }
  695. #endif
  696. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  697. if (!table)
  698. return -ENOMEM;
  699. table->mask = mask;
  700. for (count = 0; count <= mask; count++)
  701. table->flows[count].cpu = RPS_NO_CPU;
  702. } else {
  703. table = NULL;
  704. }
  705. spin_lock(&rps_dev_flow_lock);
  706. old_table = rcu_dereference_protected(queue->rps_flow_table,
  707. lockdep_is_held(&rps_dev_flow_lock));
  708. rcu_assign_pointer(queue->rps_flow_table, table);
  709. spin_unlock(&rps_dev_flow_lock);
  710. if (old_table)
  711. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  712. return len;
  713. }
  714. static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
  715. = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
  716. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
  717. = __ATTR(rps_flow_cnt, 0644,
  718. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  719. #endif /* CONFIG_RPS */
  720. static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
  721. #ifdef CONFIG_RPS
  722. &rps_cpus_attribute.attr,
  723. &rps_dev_flow_table_cnt_attribute.attr,
  724. #endif
  725. NULL
  726. };
  727. static void rx_queue_release(struct kobject *kobj)
  728. {
  729. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  730. #ifdef CONFIG_RPS
  731. struct rps_map *map;
  732. struct rps_dev_flow_table *flow_table;
  733. map = rcu_dereference_protected(queue->rps_map, 1);
  734. if (map) {
  735. RCU_INIT_POINTER(queue->rps_map, NULL);
  736. kfree_rcu(map, rcu);
  737. }
  738. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  739. if (flow_table) {
  740. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  741. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  742. }
  743. #endif
  744. memset(kobj, 0, sizeof(*kobj));
  745. dev_put(queue->dev);
  746. }
  747. static const void *rx_queue_namespace(struct kobject *kobj)
  748. {
  749. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  750. struct device *dev = &queue->dev->dev;
  751. const void *ns = NULL;
  752. if (dev->class && dev->class->ns_type)
  753. ns = dev->class->namespace(dev);
  754. return ns;
  755. }
  756. static struct kobj_type rx_queue_ktype __ro_after_init = {
  757. .sysfs_ops = &rx_queue_sysfs_ops,
  758. .release = rx_queue_release,
  759. .default_attrs = rx_queue_default_attrs,
  760. .namespace = rx_queue_namespace
  761. };
  762. static int rx_queue_add_kobject(struct net_device *dev, int index)
  763. {
  764. struct netdev_rx_queue *queue = dev->_rx + index;
  765. struct kobject *kobj = &queue->kobj;
  766. int error = 0;
  767. kobj->kset = dev->queues_kset;
  768. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  769. "rx-%u", index);
  770. if (error)
  771. return error;
  772. if (dev->sysfs_rx_queue_group) {
  773. error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
  774. if (error) {
  775. kobject_put(kobj);
  776. return error;
  777. }
  778. }
  779. kobject_uevent(kobj, KOBJ_ADD);
  780. dev_hold(queue->dev);
  781. return error;
  782. }
  783. #endif /* CONFIG_SYSFS */
  784. int
  785. net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  786. {
  787. #ifdef CONFIG_SYSFS
  788. int i;
  789. int error = 0;
  790. #ifndef CONFIG_RPS
  791. if (!dev->sysfs_rx_queue_group)
  792. return 0;
  793. #endif
  794. for (i = old_num; i < new_num; i++) {
  795. error = rx_queue_add_kobject(dev, i);
  796. if (error) {
  797. new_num = old_num;
  798. break;
  799. }
  800. }
  801. while (--i >= new_num) {
  802. struct kobject *kobj = &dev->_rx[i].kobj;
  803. if (!refcount_read(&dev_net(dev)->count))
  804. kobj->uevent_suppress = 1;
  805. if (dev->sysfs_rx_queue_group)
  806. sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
  807. kobject_put(kobj);
  808. }
  809. return error;
  810. #else
  811. return 0;
  812. #endif
  813. }
  814. #ifdef CONFIG_SYSFS
  815. /*
  816. * netdev_queue sysfs structures and functions.
  817. */
  818. struct netdev_queue_attribute {
  819. struct attribute attr;
  820. ssize_t (*show)(struct netdev_queue *queue, char *buf);
  821. ssize_t (*store)(struct netdev_queue *queue,
  822. const char *buf, size_t len);
  823. };
  824. #define to_netdev_queue_attr(_attr) \
  825. container_of(_attr, struct netdev_queue_attribute, attr)
  826. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  827. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  828. struct attribute *attr, char *buf)
  829. {
  830. const struct netdev_queue_attribute *attribute
  831. = to_netdev_queue_attr(attr);
  832. struct netdev_queue *queue = to_netdev_queue(kobj);
  833. if (!attribute->show)
  834. return -EIO;
  835. return attribute->show(queue, buf);
  836. }
  837. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  838. struct attribute *attr,
  839. const char *buf, size_t count)
  840. {
  841. const struct netdev_queue_attribute *attribute
  842. = to_netdev_queue_attr(attr);
  843. struct netdev_queue *queue = to_netdev_queue(kobj);
  844. if (!attribute->store)
  845. return -EIO;
  846. return attribute->store(queue, buf, count);
  847. }
  848. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  849. .show = netdev_queue_attr_show,
  850. .store = netdev_queue_attr_store,
  851. };
  852. static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
  853. {
  854. unsigned long trans_timeout;
  855. spin_lock_irq(&queue->_xmit_lock);
  856. trans_timeout = queue->trans_timeout;
  857. spin_unlock_irq(&queue->_xmit_lock);
  858. return sprintf(buf, "%lu", trans_timeout);
  859. }
  860. static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  861. {
  862. struct net_device *dev = queue->dev;
  863. unsigned int i;
  864. i = queue - dev->_tx;
  865. BUG_ON(i >= dev->num_tx_queues);
  866. return i;
  867. }
  868. static ssize_t traffic_class_show(struct netdev_queue *queue,
  869. char *buf)
  870. {
  871. struct net_device *dev = queue->dev;
  872. int index = get_netdev_queue_index(queue);
  873. int tc = netdev_txq_to_tc(dev, index);
  874. if (tc < 0)
  875. return -EINVAL;
  876. return sprintf(buf, "%u\n", tc);
  877. }
  878. #ifdef CONFIG_XPS
  879. static ssize_t tx_maxrate_show(struct netdev_queue *queue,
  880. char *buf)
  881. {
  882. return sprintf(buf, "%lu\n", queue->tx_maxrate);
  883. }
  884. static ssize_t tx_maxrate_store(struct netdev_queue *queue,
  885. const char *buf, size_t len)
  886. {
  887. struct net_device *dev = queue->dev;
  888. int err, index = get_netdev_queue_index(queue);
  889. u32 rate = 0;
  890. err = kstrtou32(buf, 10, &rate);
  891. if (err < 0)
  892. return err;
  893. if (!rtnl_trylock())
  894. return restart_syscall();
  895. err = -EOPNOTSUPP;
  896. if (dev->netdev_ops->ndo_set_tx_maxrate)
  897. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  898. rtnl_unlock();
  899. if (!err) {
  900. queue->tx_maxrate = rate;
  901. return len;
  902. }
  903. return err;
  904. }
  905. static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
  906. = __ATTR_RW(tx_maxrate);
  907. #endif
  908. static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
  909. = __ATTR_RO(tx_timeout);
  910. static struct netdev_queue_attribute queue_traffic_class __ro_after_init
  911. = __ATTR_RO(traffic_class);
  912. #ifdef CONFIG_BQL
  913. /*
  914. * Byte queue limits sysfs structures and functions.
  915. */
  916. static ssize_t bql_show(char *buf, unsigned int value)
  917. {
  918. return sprintf(buf, "%u\n", value);
  919. }
  920. static ssize_t bql_set(const char *buf, const size_t count,
  921. unsigned int *pvalue)
  922. {
  923. unsigned int value;
  924. int err;
  925. if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
  926. value = DQL_MAX_LIMIT;
  927. } else {
  928. err = kstrtouint(buf, 10, &value);
  929. if (err < 0)
  930. return err;
  931. if (value > DQL_MAX_LIMIT)
  932. return -EINVAL;
  933. }
  934. *pvalue = value;
  935. return count;
  936. }
  937. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  938. char *buf)
  939. {
  940. struct dql *dql = &queue->dql;
  941. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  942. }
  943. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  944. const char *buf, size_t len)
  945. {
  946. struct dql *dql = &queue->dql;
  947. unsigned int value;
  948. int err;
  949. err = kstrtouint(buf, 10, &value);
  950. if (err < 0)
  951. return err;
  952. dql->slack_hold_time = msecs_to_jiffies(value);
  953. return len;
  954. }
  955. static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
  956. = __ATTR(hold_time, 0644,
  957. bql_show_hold_time, bql_set_hold_time);
  958. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  959. char *buf)
  960. {
  961. struct dql *dql = &queue->dql;
  962. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  963. }
  964. static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
  965. __ATTR(inflight, 0444, bql_show_inflight, NULL);
  966. #define BQL_ATTR(NAME, FIELD) \
  967. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  968. char *buf) \
  969. { \
  970. return bql_show(buf, queue->dql.FIELD); \
  971. } \
  972. \
  973. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  974. const char *buf, size_t len) \
  975. { \
  976. return bql_set(buf, len, &queue->dql.FIELD); \
  977. } \
  978. \
  979. static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
  980. = __ATTR(NAME, 0644, \
  981. bql_show_ ## NAME, bql_set_ ## NAME)
  982. BQL_ATTR(limit, limit);
  983. BQL_ATTR(limit_max, max_limit);
  984. BQL_ATTR(limit_min, min_limit);
  985. static struct attribute *dql_attrs[] __ro_after_init = {
  986. &bql_limit_attribute.attr,
  987. &bql_limit_max_attribute.attr,
  988. &bql_limit_min_attribute.attr,
  989. &bql_hold_time_attribute.attr,
  990. &bql_inflight_attribute.attr,
  991. NULL
  992. };
  993. static const struct attribute_group dql_group = {
  994. .name = "byte_queue_limits",
  995. .attrs = dql_attrs,
  996. };
  997. #endif /* CONFIG_BQL */
  998. #ifdef CONFIG_XPS
  999. static ssize_t xps_cpus_show(struct netdev_queue *queue,
  1000. char *buf)
  1001. {
  1002. struct net_device *dev = queue->dev;
  1003. int cpu, len, num_tc = 1, tc = 0;
  1004. struct xps_dev_maps *dev_maps;
  1005. cpumask_var_t mask;
  1006. unsigned long index;
  1007. index = get_netdev_queue_index(queue);
  1008. if (dev->num_tc) {
  1009. num_tc = dev->num_tc;
  1010. tc = netdev_txq_to_tc(dev, index);
  1011. if (tc < 0)
  1012. return -EINVAL;
  1013. }
  1014. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  1015. return -ENOMEM;
  1016. rcu_read_lock();
  1017. dev_maps = rcu_dereference(dev->xps_cpus_map);
  1018. if (dev_maps) {
  1019. for_each_possible_cpu(cpu) {
  1020. int i, tci = cpu * num_tc + tc;
  1021. struct xps_map *map;
  1022. map = rcu_dereference(dev_maps->attr_map[tci]);
  1023. if (!map)
  1024. continue;
  1025. for (i = map->len; i--;) {
  1026. if (map->queues[i] == index) {
  1027. cpumask_set_cpu(cpu, mask);
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. rcu_read_unlock();
  1034. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  1035. free_cpumask_var(mask);
  1036. return len < PAGE_SIZE ? len : -EINVAL;
  1037. }
  1038. static ssize_t xps_cpus_store(struct netdev_queue *queue,
  1039. const char *buf, size_t len)
  1040. {
  1041. struct net_device *dev = queue->dev;
  1042. unsigned long index;
  1043. cpumask_var_t mask;
  1044. int err;
  1045. if (!capable(CAP_NET_ADMIN))
  1046. return -EPERM;
  1047. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1048. return -ENOMEM;
  1049. index = get_netdev_queue_index(queue);
  1050. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1051. if (err) {
  1052. free_cpumask_var(mask);
  1053. return err;
  1054. }
  1055. err = netif_set_xps_queue(dev, mask, index);
  1056. free_cpumask_var(mask);
  1057. return err ? : len;
  1058. }
  1059. static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
  1060. = __ATTR_RW(xps_cpus);
  1061. static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
  1062. {
  1063. struct net_device *dev = queue->dev;
  1064. struct xps_dev_maps *dev_maps;
  1065. unsigned long *mask, index;
  1066. int j, len, num_tc = 1, tc = 0;
  1067. index = get_netdev_queue_index(queue);
  1068. if (dev->num_tc) {
  1069. num_tc = dev->num_tc;
  1070. tc = netdev_txq_to_tc(dev, index);
  1071. if (tc < 0)
  1072. return -EINVAL;
  1073. }
  1074. mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
  1075. GFP_KERNEL);
  1076. if (!mask)
  1077. return -ENOMEM;
  1078. rcu_read_lock();
  1079. dev_maps = rcu_dereference(dev->xps_rxqs_map);
  1080. if (!dev_maps)
  1081. goto out_no_maps;
  1082. for (j = -1; j = netif_attrmask_next(j, NULL, dev->num_rx_queues),
  1083. j < dev->num_rx_queues;) {
  1084. int i, tci = j * num_tc + tc;
  1085. struct xps_map *map;
  1086. map = rcu_dereference(dev_maps->attr_map[tci]);
  1087. if (!map)
  1088. continue;
  1089. for (i = map->len; i--;) {
  1090. if (map->queues[i] == index) {
  1091. set_bit(j, mask);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. out_no_maps:
  1097. rcu_read_unlock();
  1098. len = bitmap_print_to_pagebuf(false, buf, mask, dev->num_rx_queues);
  1099. kfree(mask);
  1100. return len < PAGE_SIZE ? len : -EINVAL;
  1101. }
  1102. static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
  1103. size_t len)
  1104. {
  1105. struct net_device *dev = queue->dev;
  1106. struct net *net = dev_net(dev);
  1107. unsigned long *mask, index;
  1108. int err;
  1109. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1110. return -EPERM;
  1111. mask = kcalloc(BITS_TO_LONGS(dev->num_rx_queues), sizeof(long),
  1112. GFP_KERNEL);
  1113. if (!mask)
  1114. return -ENOMEM;
  1115. index = get_netdev_queue_index(queue);
  1116. err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
  1117. if (err) {
  1118. kfree(mask);
  1119. return err;
  1120. }
  1121. err = __netif_set_xps_queue(dev, mask, index, true);
  1122. kfree(mask);
  1123. return err ? : len;
  1124. }
  1125. static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
  1126. = __ATTR_RW(xps_rxqs);
  1127. #endif /* CONFIG_XPS */
  1128. static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
  1129. &queue_trans_timeout.attr,
  1130. &queue_traffic_class.attr,
  1131. #ifdef CONFIG_XPS
  1132. &xps_cpus_attribute.attr,
  1133. &xps_rxqs_attribute.attr,
  1134. &queue_tx_maxrate.attr,
  1135. #endif
  1136. NULL
  1137. };
  1138. static void netdev_queue_release(struct kobject *kobj)
  1139. {
  1140. struct netdev_queue *queue = to_netdev_queue(kobj);
  1141. memset(kobj, 0, sizeof(*kobj));
  1142. dev_put(queue->dev);
  1143. }
  1144. static const void *netdev_queue_namespace(struct kobject *kobj)
  1145. {
  1146. struct netdev_queue *queue = to_netdev_queue(kobj);
  1147. struct device *dev = &queue->dev->dev;
  1148. const void *ns = NULL;
  1149. if (dev->class && dev->class->ns_type)
  1150. ns = dev->class->namespace(dev);
  1151. return ns;
  1152. }
  1153. static struct kobj_type netdev_queue_ktype __ro_after_init = {
  1154. .sysfs_ops = &netdev_queue_sysfs_ops,
  1155. .release = netdev_queue_release,
  1156. .default_attrs = netdev_queue_default_attrs,
  1157. .namespace = netdev_queue_namespace,
  1158. };
  1159. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1160. {
  1161. struct netdev_queue *queue = dev->_tx + index;
  1162. struct kobject *kobj = &queue->kobj;
  1163. int error = 0;
  1164. kobj->kset = dev->queues_kset;
  1165. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1166. "tx-%u", index);
  1167. if (error)
  1168. return error;
  1169. #ifdef CONFIG_BQL
  1170. error = sysfs_create_group(kobj, &dql_group);
  1171. if (error) {
  1172. kobject_put(kobj);
  1173. return error;
  1174. }
  1175. #endif
  1176. kobject_uevent(kobj, KOBJ_ADD);
  1177. dev_hold(queue->dev);
  1178. return 0;
  1179. }
  1180. #endif /* CONFIG_SYSFS */
  1181. int
  1182. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1183. {
  1184. #ifdef CONFIG_SYSFS
  1185. int i;
  1186. int error = 0;
  1187. for (i = old_num; i < new_num; i++) {
  1188. error = netdev_queue_add_kobject(dev, i);
  1189. if (error) {
  1190. new_num = old_num;
  1191. break;
  1192. }
  1193. }
  1194. while (--i >= new_num) {
  1195. struct netdev_queue *queue = dev->_tx + i;
  1196. if (!refcount_read(&dev_net(dev)->count))
  1197. queue->kobj.uevent_suppress = 1;
  1198. #ifdef CONFIG_BQL
  1199. sysfs_remove_group(&queue->kobj, &dql_group);
  1200. #endif
  1201. kobject_put(&queue->kobj);
  1202. }
  1203. return error;
  1204. #else
  1205. return 0;
  1206. #endif /* CONFIG_SYSFS */
  1207. }
  1208. static int register_queue_kobjects(struct net_device *dev)
  1209. {
  1210. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1211. #ifdef CONFIG_SYSFS
  1212. dev->queues_kset = kset_create_and_add("queues",
  1213. NULL, &dev->dev.kobj);
  1214. if (!dev->queues_kset)
  1215. return -ENOMEM;
  1216. real_rx = dev->real_num_rx_queues;
  1217. #endif
  1218. real_tx = dev->real_num_tx_queues;
  1219. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1220. if (error)
  1221. goto error;
  1222. rxq = real_rx;
  1223. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1224. if (error)
  1225. goto error;
  1226. txq = real_tx;
  1227. return 0;
  1228. error:
  1229. netdev_queue_update_kobjects(dev, txq, 0);
  1230. net_rx_queue_update_kobjects(dev, rxq, 0);
  1231. return error;
  1232. }
  1233. static void remove_queue_kobjects(struct net_device *dev)
  1234. {
  1235. int real_rx = 0, real_tx = 0;
  1236. #ifdef CONFIG_SYSFS
  1237. real_rx = dev->real_num_rx_queues;
  1238. #endif
  1239. real_tx = dev->real_num_tx_queues;
  1240. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1241. netdev_queue_update_kobjects(dev, real_tx, 0);
  1242. #ifdef CONFIG_SYSFS
  1243. kset_unregister(dev->queues_kset);
  1244. #endif
  1245. }
  1246. static bool net_current_may_mount(void)
  1247. {
  1248. struct net *net = current->nsproxy->net_ns;
  1249. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1250. }
  1251. static void *net_grab_current_ns(void)
  1252. {
  1253. struct net *ns = current->nsproxy->net_ns;
  1254. #ifdef CONFIG_NET_NS
  1255. if (ns)
  1256. refcount_inc(&ns->passive);
  1257. #endif
  1258. return ns;
  1259. }
  1260. static const void *net_initial_ns(void)
  1261. {
  1262. return &init_net;
  1263. }
  1264. static const void *net_netlink_ns(struct sock *sk)
  1265. {
  1266. return sock_net(sk);
  1267. }
  1268. const struct kobj_ns_type_operations net_ns_type_operations = {
  1269. .type = KOBJ_NS_TYPE_NET,
  1270. .current_may_mount = net_current_may_mount,
  1271. .grab_current_ns = net_grab_current_ns,
  1272. .netlink_ns = net_netlink_ns,
  1273. .initial_ns = net_initial_ns,
  1274. .drop_ns = net_drop_ns,
  1275. };
  1276. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1277. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1278. {
  1279. struct net_device *dev = to_net_dev(d);
  1280. int retval;
  1281. /* pass interface to uevent. */
  1282. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1283. if (retval)
  1284. goto exit;
  1285. /* pass ifindex to uevent.
  1286. * ifindex is useful as it won't change (interface name may change)
  1287. * and is what RtNetlink uses natively.
  1288. */
  1289. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1290. exit:
  1291. return retval;
  1292. }
  1293. /*
  1294. * netdev_release -- destroy and free a dead device.
  1295. * Called when last reference to device kobject is gone.
  1296. */
  1297. static void netdev_release(struct device *d)
  1298. {
  1299. struct net_device *dev = to_net_dev(d);
  1300. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1301. /* no need to wait for rcu grace period:
  1302. * device is dead and about to be freed.
  1303. */
  1304. kfree(rcu_access_pointer(dev->ifalias));
  1305. netdev_freemem(dev);
  1306. }
  1307. static const void *net_namespace(struct device *d)
  1308. {
  1309. struct net_device *dev = to_net_dev(d);
  1310. return dev_net(dev);
  1311. }
  1312. static struct class net_class __ro_after_init = {
  1313. .name = "net",
  1314. .dev_release = netdev_release,
  1315. .dev_groups = net_class_groups,
  1316. .dev_uevent = netdev_uevent,
  1317. .ns_type = &net_ns_type_operations,
  1318. .namespace = net_namespace,
  1319. };
  1320. #ifdef CONFIG_OF_NET
  1321. static int of_dev_node_match(struct device *dev, const void *data)
  1322. {
  1323. int ret = 0;
  1324. if (dev->parent)
  1325. ret = dev->parent->of_node == data;
  1326. return ret == 0 ? dev->of_node == data : ret;
  1327. }
  1328. /*
  1329. * of_find_net_device_by_node - lookup the net device for the device node
  1330. * @np: OF device node
  1331. *
  1332. * Looks up the net_device structure corresponding with the device node.
  1333. * If successful, returns a pointer to the net_device with the embedded
  1334. * struct device refcount incremented by one, or NULL on failure. The
  1335. * refcount must be dropped when done with the net_device.
  1336. */
  1337. struct net_device *of_find_net_device_by_node(struct device_node *np)
  1338. {
  1339. struct device *dev;
  1340. dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
  1341. if (!dev)
  1342. return NULL;
  1343. return to_net_dev(dev);
  1344. }
  1345. EXPORT_SYMBOL(of_find_net_device_by_node);
  1346. #endif
  1347. /* Delete sysfs entries but hold kobject reference until after all
  1348. * netdev references are gone.
  1349. */
  1350. void netdev_unregister_kobject(struct net_device *ndev)
  1351. {
  1352. struct device *dev = &ndev->dev;
  1353. if (!refcount_read(&dev_net(ndev)->count))
  1354. dev_set_uevent_suppress(dev, 1);
  1355. kobject_get(&dev->kobj);
  1356. remove_queue_kobjects(ndev);
  1357. pm_runtime_set_memalloc_noio(dev, false);
  1358. device_del(dev);
  1359. }
  1360. /* Create sysfs entries for network device. */
  1361. int netdev_register_kobject(struct net_device *ndev)
  1362. {
  1363. struct device *dev = &ndev->dev;
  1364. const struct attribute_group **groups = ndev->sysfs_groups;
  1365. int error = 0;
  1366. device_initialize(dev);
  1367. dev->class = &net_class;
  1368. dev->platform_data = ndev;
  1369. dev->groups = groups;
  1370. dev_set_name(dev, "%s", ndev->name);
  1371. #ifdef CONFIG_SYSFS
  1372. /* Allow for a device specific group */
  1373. if (*groups)
  1374. groups++;
  1375. *groups++ = &netstat_group;
  1376. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  1377. if (ndev->ieee80211_ptr)
  1378. *groups++ = &wireless_group;
  1379. #if IS_ENABLED(CONFIG_WIRELESS_EXT)
  1380. else if (ndev->wireless_handlers)
  1381. *groups++ = &wireless_group;
  1382. #endif
  1383. #endif
  1384. #endif /* CONFIG_SYSFS */
  1385. error = device_add(dev);
  1386. if (error)
  1387. return error;
  1388. error = register_queue_kobjects(ndev);
  1389. if (error) {
  1390. device_del(dev);
  1391. return error;
  1392. }
  1393. pm_runtime_set_memalloc_noio(dev, true);
  1394. return error;
  1395. }
  1396. int netdev_class_create_file_ns(const struct class_attribute *class_attr,
  1397. const void *ns)
  1398. {
  1399. return class_create_file_ns(&net_class, class_attr, ns);
  1400. }
  1401. EXPORT_SYMBOL(netdev_class_create_file_ns);
  1402. void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
  1403. const void *ns)
  1404. {
  1405. class_remove_file_ns(&net_class, class_attr, ns);
  1406. }
  1407. EXPORT_SYMBOL(netdev_class_remove_file_ns);
  1408. int __init netdev_kobject_init(void)
  1409. {
  1410. kobj_ns_type_register(&net_ns_type_operations);
  1411. return class_register(&net_class);
  1412. }