net-sysfs.c 38 KB

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