net-sysfs.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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 <linux/if_arp.h>
  15. #include <linux/slab.h>
  16. #include <linux/nsproxy.h>
  17. #include <net/sock.h>
  18. #include <net/net_namespace.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/export.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/pm_runtime.h>
  24. #include "net-sysfs.h"
  25. #ifdef CONFIG_SYSFS
  26. static const char fmt_hex[] = "%#x\n";
  27. static const char fmt_long_hex[] = "%#lx\n";
  28. static const char fmt_dec[] = "%d\n";
  29. static const char fmt_udec[] = "%u\n";
  30. static const char fmt_ulong[] = "%lu\n";
  31. static const char fmt_u64[] = "%llu\n";
  32. static inline int dev_isalive(const struct net_device *dev)
  33. {
  34. return dev->reg_state <= NETREG_REGISTERED;
  35. }
  36. /* use same locking rules as GIF* ioctl's */
  37. static ssize_t netdev_show(const struct device *dev,
  38. struct device_attribute *attr, char *buf,
  39. ssize_t (*format)(const struct net_device *, char *))
  40. {
  41. struct net_device *net = to_net_dev(dev);
  42. ssize_t ret = -EINVAL;
  43. read_lock(&dev_base_lock);
  44. if (dev_isalive(net))
  45. ret = (*format)(net, buf);
  46. read_unlock(&dev_base_lock);
  47. return ret;
  48. }
  49. /* generate a show function for simple field */
  50. #define NETDEVICE_SHOW(field, format_string) \
  51. static ssize_t format_##field(const struct net_device *net, char *buf) \
  52. { \
  53. return sprintf(buf, format_string, net->field); \
  54. } \
  55. static ssize_t show_##field(struct device *dev, \
  56. struct device_attribute *attr, char *buf) \
  57. { \
  58. return netdev_show(dev, attr, buf, format_##field); \
  59. }
  60. /* use same locking and permission rules as SIF* ioctl's */
  61. static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  62. const char *buf, size_t len,
  63. int (*set)(struct net_device *, unsigned long))
  64. {
  65. struct net_device *netdev = to_net_dev(dev);
  66. struct net *net = dev_net(netdev);
  67. unsigned long new;
  68. int ret = -EINVAL;
  69. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  70. return -EPERM;
  71. ret = kstrtoul(buf, 0, &new);
  72. if (ret)
  73. goto err;
  74. if (!rtnl_trylock())
  75. return restart_syscall();
  76. if (dev_isalive(netdev)) {
  77. if ((ret = (*set)(netdev, new)) == 0)
  78. ret = len;
  79. }
  80. rtnl_unlock();
  81. err:
  82. return ret;
  83. }
  84. NETDEVICE_SHOW(dev_id, fmt_hex);
  85. NETDEVICE_SHOW(addr_assign_type, fmt_dec);
  86. NETDEVICE_SHOW(addr_len, fmt_dec);
  87. NETDEVICE_SHOW(iflink, fmt_dec);
  88. NETDEVICE_SHOW(ifindex, fmt_dec);
  89. NETDEVICE_SHOW(type, fmt_dec);
  90. NETDEVICE_SHOW(link_mode, fmt_dec);
  91. /* use same locking rules as GIFHWADDR ioctl's */
  92. static ssize_t show_address(struct device *dev, struct device_attribute *attr,
  93. char *buf)
  94. {
  95. struct net_device *net = to_net_dev(dev);
  96. ssize_t ret = -EINVAL;
  97. read_lock(&dev_base_lock);
  98. if (dev_isalive(net))
  99. ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
  100. read_unlock(&dev_base_lock);
  101. return ret;
  102. }
  103. static ssize_t show_broadcast(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct net_device *net = to_net_dev(dev);
  107. if (dev_isalive(net))
  108. return sysfs_format_mac(buf, net->broadcast, net->addr_len);
  109. return -EINVAL;
  110. }
  111. static int change_carrier(struct net_device *net, unsigned long new_carrier)
  112. {
  113. if (!netif_running(net))
  114. return -EINVAL;
  115. return dev_change_carrier(net, (bool) new_carrier);
  116. }
  117. static ssize_t store_carrier(struct device *dev, struct device_attribute *attr,
  118. const char *buf, size_t len)
  119. {
  120. return netdev_store(dev, attr, buf, len, change_carrier);
  121. }
  122. static ssize_t show_carrier(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. struct net_device *netdev = to_net_dev(dev);
  126. if (netif_running(netdev)) {
  127. return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
  128. }
  129. return -EINVAL;
  130. }
  131. static ssize_t show_speed(struct device *dev,
  132. struct device_attribute *attr, char *buf)
  133. {
  134. struct net_device *netdev = to_net_dev(dev);
  135. int ret = -EINVAL;
  136. if (!rtnl_trylock())
  137. return restart_syscall();
  138. if (netif_running(netdev)) {
  139. struct ethtool_cmd cmd;
  140. if (!__ethtool_get_settings(netdev, &cmd))
  141. ret = sprintf(buf, fmt_udec, ethtool_cmd_speed(&cmd));
  142. }
  143. rtnl_unlock();
  144. return ret;
  145. }
  146. static ssize_t show_duplex(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct net_device *netdev = to_net_dev(dev);
  150. int ret = -EINVAL;
  151. if (!rtnl_trylock())
  152. return restart_syscall();
  153. if (netif_running(netdev)) {
  154. struct ethtool_cmd cmd;
  155. if (!__ethtool_get_settings(netdev, &cmd)) {
  156. const char *duplex;
  157. switch (cmd.duplex) {
  158. case DUPLEX_HALF:
  159. duplex = "half";
  160. break;
  161. case DUPLEX_FULL:
  162. duplex = "full";
  163. break;
  164. default:
  165. duplex = "unknown";
  166. break;
  167. }
  168. ret = sprintf(buf, "%s\n", duplex);
  169. }
  170. }
  171. rtnl_unlock();
  172. return ret;
  173. }
  174. static ssize_t show_dormant(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct net_device *netdev = to_net_dev(dev);
  178. if (netif_running(netdev))
  179. return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
  180. return -EINVAL;
  181. }
  182. static const char *const operstates[] = {
  183. "unknown",
  184. "notpresent", /* currently unused */
  185. "down",
  186. "lowerlayerdown",
  187. "testing", /* currently unused */
  188. "dormant",
  189. "up"
  190. };
  191. static ssize_t show_operstate(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. const struct net_device *netdev = to_net_dev(dev);
  195. unsigned char operstate;
  196. read_lock(&dev_base_lock);
  197. operstate = netdev->operstate;
  198. if (!netif_running(netdev))
  199. operstate = IF_OPER_DOWN;
  200. read_unlock(&dev_base_lock);
  201. if (operstate >= ARRAY_SIZE(operstates))
  202. return -EINVAL; /* should not happen */
  203. return sprintf(buf, "%s\n", operstates[operstate]);
  204. }
  205. /* read-write attributes */
  206. NETDEVICE_SHOW(mtu, fmt_dec);
  207. static int change_mtu(struct net_device *net, unsigned long new_mtu)
  208. {
  209. return dev_set_mtu(net, (int) new_mtu);
  210. }
  211. static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
  212. const char *buf, size_t len)
  213. {
  214. return netdev_store(dev, attr, buf, len, change_mtu);
  215. }
  216. NETDEVICE_SHOW(flags, fmt_hex);
  217. static int change_flags(struct net_device *net, unsigned long new_flags)
  218. {
  219. return dev_change_flags(net, (unsigned int) new_flags);
  220. }
  221. static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
  222. const char *buf, size_t len)
  223. {
  224. return netdev_store(dev, attr, buf, len, change_flags);
  225. }
  226. NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
  227. static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
  228. {
  229. net->tx_queue_len = new_len;
  230. return 0;
  231. }
  232. static ssize_t store_tx_queue_len(struct device *dev,
  233. struct device_attribute *attr,
  234. const char *buf, size_t len)
  235. {
  236. if (!capable(CAP_NET_ADMIN))
  237. return -EPERM;
  238. return netdev_store(dev, attr, buf, len, change_tx_queue_len);
  239. }
  240. static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
  241. const char *buf, size_t len)
  242. {
  243. struct net_device *netdev = to_net_dev(dev);
  244. struct net *net = dev_net(netdev);
  245. size_t count = len;
  246. ssize_t ret;
  247. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  248. return -EPERM;
  249. /* ignore trailing newline */
  250. if (len > 0 && buf[len - 1] == '\n')
  251. --count;
  252. if (!rtnl_trylock())
  253. return restart_syscall();
  254. ret = dev_set_alias(netdev, buf, count);
  255. rtnl_unlock();
  256. return ret < 0 ? ret : len;
  257. }
  258. static ssize_t show_ifalias(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. const struct net_device *netdev = to_net_dev(dev);
  262. ssize_t ret = 0;
  263. if (!rtnl_trylock())
  264. return restart_syscall();
  265. if (netdev->ifalias)
  266. ret = sprintf(buf, "%s\n", netdev->ifalias);
  267. rtnl_unlock();
  268. return ret;
  269. }
  270. NETDEVICE_SHOW(group, fmt_dec);
  271. static int change_group(struct net_device *net, unsigned long new_group)
  272. {
  273. dev_set_group(net, (int) new_group);
  274. return 0;
  275. }
  276. static ssize_t store_group(struct device *dev, struct device_attribute *attr,
  277. const char *buf, size_t len)
  278. {
  279. return netdev_store(dev, attr, buf, len, change_group);
  280. }
  281. static struct device_attribute net_class_attributes[] = {
  282. __ATTR(addr_assign_type, S_IRUGO, show_addr_assign_type, NULL),
  283. __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
  284. __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
  285. __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
  286. __ATTR(iflink, S_IRUGO, show_iflink, NULL),
  287. __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
  288. __ATTR(type, S_IRUGO, show_type, NULL),
  289. __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
  290. __ATTR(address, S_IRUGO, show_address, NULL),
  291. __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
  292. __ATTR(carrier, S_IRUGO | S_IWUSR, show_carrier, store_carrier),
  293. __ATTR(speed, S_IRUGO, show_speed, NULL),
  294. __ATTR(duplex, S_IRUGO, show_duplex, NULL),
  295. __ATTR(dormant, S_IRUGO, show_dormant, NULL),
  296. __ATTR(operstate, S_IRUGO, show_operstate, NULL),
  297. __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
  298. __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
  299. __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
  300. store_tx_queue_len),
  301. __ATTR(netdev_group, S_IRUGO | S_IWUSR, show_group, store_group),
  302. {}
  303. };
  304. /* Show a given an attribute in the statistics group */
  305. static ssize_t netstat_show(const struct device *d,
  306. struct device_attribute *attr, char *buf,
  307. unsigned long offset)
  308. {
  309. struct net_device *dev = to_net_dev(d);
  310. ssize_t ret = -EINVAL;
  311. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  312. offset % sizeof(u64) != 0);
  313. read_lock(&dev_base_lock);
  314. if (dev_isalive(dev)) {
  315. struct rtnl_link_stats64 temp;
  316. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  317. ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
  318. }
  319. read_unlock(&dev_base_lock);
  320. return ret;
  321. }
  322. /* generate a read-only statistics attribute */
  323. #define NETSTAT_ENTRY(name) \
  324. static ssize_t show_##name(struct device *d, \
  325. struct device_attribute *attr, char *buf) \
  326. { \
  327. return netstat_show(d, attr, buf, \
  328. offsetof(struct rtnl_link_stats64, name)); \
  329. } \
  330. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  331. NETSTAT_ENTRY(rx_packets);
  332. NETSTAT_ENTRY(tx_packets);
  333. NETSTAT_ENTRY(rx_bytes);
  334. NETSTAT_ENTRY(tx_bytes);
  335. NETSTAT_ENTRY(rx_errors);
  336. NETSTAT_ENTRY(tx_errors);
  337. NETSTAT_ENTRY(rx_dropped);
  338. NETSTAT_ENTRY(tx_dropped);
  339. NETSTAT_ENTRY(multicast);
  340. NETSTAT_ENTRY(collisions);
  341. NETSTAT_ENTRY(rx_length_errors);
  342. NETSTAT_ENTRY(rx_over_errors);
  343. NETSTAT_ENTRY(rx_crc_errors);
  344. NETSTAT_ENTRY(rx_frame_errors);
  345. NETSTAT_ENTRY(rx_fifo_errors);
  346. NETSTAT_ENTRY(rx_missed_errors);
  347. NETSTAT_ENTRY(tx_aborted_errors);
  348. NETSTAT_ENTRY(tx_carrier_errors);
  349. NETSTAT_ENTRY(tx_fifo_errors);
  350. NETSTAT_ENTRY(tx_heartbeat_errors);
  351. NETSTAT_ENTRY(tx_window_errors);
  352. NETSTAT_ENTRY(rx_compressed);
  353. NETSTAT_ENTRY(tx_compressed);
  354. static struct attribute *netstat_attrs[] = {
  355. &dev_attr_rx_packets.attr,
  356. &dev_attr_tx_packets.attr,
  357. &dev_attr_rx_bytes.attr,
  358. &dev_attr_tx_bytes.attr,
  359. &dev_attr_rx_errors.attr,
  360. &dev_attr_tx_errors.attr,
  361. &dev_attr_rx_dropped.attr,
  362. &dev_attr_tx_dropped.attr,
  363. &dev_attr_multicast.attr,
  364. &dev_attr_collisions.attr,
  365. &dev_attr_rx_length_errors.attr,
  366. &dev_attr_rx_over_errors.attr,
  367. &dev_attr_rx_crc_errors.attr,
  368. &dev_attr_rx_frame_errors.attr,
  369. &dev_attr_rx_fifo_errors.attr,
  370. &dev_attr_rx_missed_errors.attr,
  371. &dev_attr_tx_aborted_errors.attr,
  372. &dev_attr_tx_carrier_errors.attr,
  373. &dev_attr_tx_fifo_errors.attr,
  374. &dev_attr_tx_heartbeat_errors.attr,
  375. &dev_attr_tx_window_errors.attr,
  376. &dev_attr_rx_compressed.attr,
  377. &dev_attr_tx_compressed.attr,
  378. NULL
  379. };
  380. static struct attribute_group netstat_group = {
  381. .name = "statistics",
  382. .attrs = netstat_attrs,
  383. };
  384. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  385. static struct attribute *wireless_attrs[] = {
  386. NULL
  387. };
  388. static struct attribute_group wireless_group = {
  389. .name = "wireless",
  390. .attrs = wireless_attrs,
  391. };
  392. #endif
  393. #endif /* CONFIG_SYSFS */
  394. #ifdef CONFIG_RPS
  395. /*
  396. * RX queue sysfs structures and functions.
  397. */
  398. struct rx_queue_attribute {
  399. struct attribute attr;
  400. ssize_t (*show)(struct netdev_rx_queue *queue,
  401. struct rx_queue_attribute *attr, char *buf);
  402. ssize_t (*store)(struct netdev_rx_queue *queue,
  403. struct rx_queue_attribute *attr, const char *buf, size_t len);
  404. };
  405. #define to_rx_queue_attr(_attr) container_of(_attr, \
  406. struct rx_queue_attribute, attr)
  407. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  408. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  409. char *buf)
  410. {
  411. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  412. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  413. if (!attribute->show)
  414. return -EIO;
  415. return attribute->show(queue, attribute, buf);
  416. }
  417. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  421. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  422. if (!attribute->store)
  423. return -EIO;
  424. return attribute->store(queue, attribute, buf, count);
  425. }
  426. static const struct sysfs_ops rx_queue_sysfs_ops = {
  427. .show = rx_queue_attr_show,
  428. .store = rx_queue_attr_store,
  429. };
  430. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  431. struct rx_queue_attribute *attribute, char *buf)
  432. {
  433. struct rps_map *map;
  434. cpumask_var_t mask;
  435. size_t len = 0;
  436. int i;
  437. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  438. return -ENOMEM;
  439. rcu_read_lock();
  440. map = rcu_dereference(queue->rps_map);
  441. if (map)
  442. for (i = 0; i < map->len; i++)
  443. cpumask_set_cpu(map->cpus[i], mask);
  444. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  445. if (PAGE_SIZE - len < 3) {
  446. rcu_read_unlock();
  447. free_cpumask_var(mask);
  448. return -EINVAL;
  449. }
  450. rcu_read_unlock();
  451. free_cpumask_var(mask);
  452. len += sprintf(buf + len, "\n");
  453. return len;
  454. }
  455. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  456. struct rx_queue_attribute *attribute,
  457. const char *buf, size_t len)
  458. {
  459. struct rps_map *old_map, *map;
  460. cpumask_var_t mask;
  461. int err, cpu, i;
  462. static DEFINE_SPINLOCK(rps_map_lock);
  463. if (!capable(CAP_NET_ADMIN))
  464. return -EPERM;
  465. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  466. return -ENOMEM;
  467. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  468. if (err) {
  469. free_cpumask_var(mask);
  470. return err;
  471. }
  472. map = kzalloc(max_t(unsigned int,
  473. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  474. GFP_KERNEL);
  475. if (!map) {
  476. free_cpumask_var(mask);
  477. return -ENOMEM;
  478. }
  479. i = 0;
  480. for_each_cpu_and(cpu, mask, cpu_online_mask)
  481. map->cpus[i++] = cpu;
  482. if (i)
  483. map->len = i;
  484. else {
  485. kfree(map);
  486. map = NULL;
  487. }
  488. spin_lock(&rps_map_lock);
  489. old_map = rcu_dereference_protected(queue->rps_map,
  490. lockdep_is_held(&rps_map_lock));
  491. rcu_assign_pointer(queue->rps_map, map);
  492. spin_unlock(&rps_map_lock);
  493. if (map)
  494. static_key_slow_inc(&rps_needed);
  495. if (old_map) {
  496. kfree_rcu(old_map, rcu);
  497. static_key_slow_dec(&rps_needed);
  498. }
  499. free_cpumask_var(mask);
  500. return len;
  501. }
  502. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  503. struct rx_queue_attribute *attr,
  504. char *buf)
  505. {
  506. struct rps_dev_flow_table *flow_table;
  507. unsigned long val = 0;
  508. rcu_read_lock();
  509. flow_table = rcu_dereference(queue->rps_flow_table);
  510. if (flow_table)
  511. val = (unsigned long)flow_table->mask + 1;
  512. rcu_read_unlock();
  513. return sprintf(buf, "%lu\n", val);
  514. }
  515. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  516. {
  517. struct rps_dev_flow_table *table = container_of(rcu,
  518. struct rps_dev_flow_table, rcu);
  519. vfree(table);
  520. }
  521. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  522. struct rx_queue_attribute *attr,
  523. const char *buf, size_t len)
  524. {
  525. unsigned long mask, count;
  526. struct rps_dev_flow_table *table, *old_table;
  527. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  528. int rc;
  529. if (!capable(CAP_NET_ADMIN))
  530. return -EPERM;
  531. rc = kstrtoul(buf, 0, &count);
  532. if (rc < 0)
  533. return rc;
  534. if (count) {
  535. mask = count - 1;
  536. /* mask = roundup_pow_of_two(count) - 1;
  537. * without overflows...
  538. */
  539. while ((mask | (mask >> 1)) != mask)
  540. mask |= (mask >> 1);
  541. /* On 64 bit arches, must check mask fits in table->mask (u32),
  542. * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
  543. * doesnt overflow.
  544. */
  545. #if BITS_PER_LONG > 32
  546. if (mask > (unsigned long)(u32)mask)
  547. return -EINVAL;
  548. #else
  549. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  550. / sizeof(struct rps_dev_flow)) {
  551. /* Enforce a limit to prevent overflow */
  552. return -EINVAL;
  553. }
  554. #endif
  555. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  556. if (!table)
  557. return -ENOMEM;
  558. table->mask = mask;
  559. for (count = 0; count <= mask; count++)
  560. table->flows[count].cpu = RPS_NO_CPU;
  561. } else
  562. table = NULL;
  563. spin_lock(&rps_dev_flow_lock);
  564. old_table = rcu_dereference_protected(queue->rps_flow_table,
  565. lockdep_is_held(&rps_dev_flow_lock));
  566. rcu_assign_pointer(queue->rps_flow_table, table);
  567. spin_unlock(&rps_dev_flow_lock);
  568. if (old_table)
  569. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  570. return len;
  571. }
  572. static struct rx_queue_attribute rps_cpus_attribute =
  573. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  574. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  575. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  576. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  577. static struct attribute *rx_queue_default_attrs[] = {
  578. &rps_cpus_attribute.attr,
  579. &rps_dev_flow_table_cnt_attribute.attr,
  580. NULL
  581. };
  582. static void rx_queue_release(struct kobject *kobj)
  583. {
  584. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  585. struct rps_map *map;
  586. struct rps_dev_flow_table *flow_table;
  587. map = rcu_dereference_protected(queue->rps_map, 1);
  588. if (map) {
  589. RCU_INIT_POINTER(queue->rps_map, NULL);
  590. kfree_rcu(map, rcu);
  591. }
  592. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  593. if (flow_table) {
  594. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  595. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  596. }
  597. memset(kobj, 0, sizeof(*kobj));
  598. dev_put(queue->dev);
  599. }
  600. static struct kobj_type rx_queue_ktype = {
  601. .sysfs_ops = &rx_queue_sysfs_ops,
  602. .release = rx_queue_release,
  603. .default_attrs = rx_queue_default_attrs,
  604. };
  605. static int rx_queue_add_kobject(struct net_device *net, int index)
  606. {
  607. struct netdev_rx_queue *queue = net->_rx + index;
  608. struct kobject *kobj = &queue->kobj;
  609. int error = 0;
  610. kobj->kset = net->queues_kset;
  611. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  612. "rx-%u", index);
  613. if (error) {
  614. kobject_put(kobj);
  615. return error;
  616. }
  617. kobject_uevent(kobj, KOBJ_ADD);
  618. dev_hold(queue->dev);
  619. return error;
  620. }
  621. #endif /* CONFIG_RPS */
  622. int
  623. net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  624. {
  625. #ifdef CONFIG_RPS
  626. int i;
  627. int error = 0;
  628. for (i = old_num; i < new_num; i++) {
  629. error = rx_queue_add_kobject(net, i);
  630. if (error) {
  631. new_num = old_num;
  632. break;
  633. }
  634. }
  635. while (--i >= new_num)
  636. kobject_put(&net->_rx[i].kobj);
  637. return error;
  638. #else
  639. return 0;
  640. #endif
  641. }
  642. #ifdef CONFIG_SYSFS
  643. /*
  644. * netdev_queue sysfs structures and functions.
  645. */
  646. struct netdev_queue_attribute {
  647. struct attribute attr;
  648. ssize_t (*show)(struct netdev_queue *queue,
  649. struct netdev_queue_attribute *attr, char *buf);
  650. ssize_t (*store)(struct netdev_queue *queue,
  651. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  652. };
  653. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  654. struct netdev_queue_attribute, attr)
  655. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  656. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  657. struct attribute *attr, char *buf)
  658. {
  659. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  660. struct netdev_queue *queue = to_netdev_queue(kobj);
  661. if (!attribute->show)
  662. return -EIO;
  663. return attribute->show(queue, attribute, buf);
  664. }
  665. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  666. struct attribute *attr,
  667. const char *buf, size_t count)
  668. {
  669. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  670. struct netdev_queue *queue = to_netdev_queue(kobj);
  671. if (!attribute->store)
  672. return -EIO;
  673. return attribute->store(queue, attribute, buf, count);
  674. }
  675. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  676. .show = netdev_queue_attr_show,
  677. .store = netdev_queue_attr_store,
  678. };
  679. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  680. struct netdev_queue_attribute *attribute,
  681. char *buf)
  682. {
  683. unsigned long trans_timeout;
  684. spin_lock_irq(&queue->_xmit_lock);
  685. trans_timeout = queue->trans_timeout;
  686. spin_unlock_irq(&queue->_xmit_lock);
  687. return sprintf(buf, "%lu", trans_timeout);
  688. }
  689. static struct netdev_queue_attribute queue_trans_timeout =
  690. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  691. #ifdef CONFIG_BQL
  692. /*
  693. * Byte queue limits sysfs structures and functions.
  694. */
  695. static ssize_t bql_show(char *buf, unsigned int value)
  696. {
  697. return sprintf(buf, "%u\n", value);
  698. }
  699. static ssize_t bql_set(const char *buf, const size_t count,
  700. unsigned int *pvalue)
  701. {
  702. unsigned int value;
  703. int err;
  704. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  705. value = DQL_MAX_LIMIT;
  706. else {
  707. err = kstrtouint(buf, 10, &value);
  708. if (err < 0)
  709. return err;
  710. if (value > DQL_MAX_LIMIT)
  711. return -EINVAL;
  712. }
  713. *pvalue = value;
  714. return count;
  715. }
  716. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  717. struct netdev_queue_attribute *attr,
  718. char *buf)
  719. {
  720. struct dql *dql = &queue->dql;
  721. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  722. }
  723. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  724. struct netdev_queue_attribute *attribute,
  725. const char *buf, size_t len)
  726. {
  727. struct dql *dql = &queue->dql;
  728. unsigned int value;
  729. int err;
  730. err = kstrtouint(buf, 10, &value);
  731. if (err < 0)
  732. return err;
  733. dql->slack_hold_time = msecs_to_jiffies(value);
  734. return len;
  735. }
  736. static struct netdev_queue_attribute bql_hold_time_attribute =
  737. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  738. bql_set_hold_time);
  739. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  740. struct netdev_queue_attribute *attr,
  741. char *buf)
  742. {
  743. struct dql *dql = &queue->dql;
  744. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  745. }
  746. static struct netdev_queue_attribute bql_inflight_attribute =
  747. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  748. #define BQL_ATTR(NAME, FIELD) \
  749. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  750. struct netdev_queue_attribute *attr, \
  751. char *buf) \
  752. { \
  753. return bql_show(buf, queue->dql.FIELD); \
  754. } \
  755. \
  756. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  757. struct netdev_queue_attribute *attr, \
  758. const char *buf, size_t len) \
  759. { \
  760. return bql_set(buf, len, &queue->dql.FIELD); \
  761. } \
  762. \
  763. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  764. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  765. bql_set_ ## NAME);
  766. BQL_ATTR(limit, limit)
  767. BQL_ATTR(limit_max, max_limit)
  768. BQL_ATTR(limit_min, min_limit)
  769. static struct attribute *dql_attrs[] = {
  770. &bql_limit_attribute.attr,
  771. &bql_limit_max_attribute.attr,
  772. &bql_limit_min_attribute.attr,
  773. &bql_hold_time_attribute.attr,
  774. &bql_inflight_attribute.attr,
  775. NULL
  776. };
  777. static struct attribute_group dql_group = {
  778. .name = "byte_queue_limits",
  779. .attrs = dql_attrs,
  780. };
  781. #endif /* CONFIG_BQL */
  782. #ifdef CONFIG_XPS
  783. static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  784. {
  785. struct net_device *dev = queue->dev;
  786. int i;
  787. for (i = 0; i < dev->num_tx_queues; i++)
  788. if (queue == &dev->_tx[i])
  789. break;
  790. BUG_ON(i >= dev->num_tx_queues);
  791. return i;
  792. }
  793. static ssize_t show_xps_map(struct netdev_queue *queue,
  794. struct netdev_queue_attribute *attribute, char *buf)
  795. {
  796. struct net_device *dev = queue->dev;
  797. struct xps_dev_maps *dev_maps;
  798. cpumask_var_t mask;
  799. unsigned long index;
  800. size_t len = 0;
  801. int i;
  802. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  803. return -ENOMEM;
  804. index = get_netdev_queue_index(queue);
  805. rcu_read_lock();
  806. dev_maps = rcu_dereference(dev->xps_maps);
  807. if (dev_maps) {
  808. for_each_possible_cpu(i) {
  809. struct xps_map *map =
  810. rcu_dereference(dev_maps->cpu_map[i]);
  811. if (map) {
  812. int j;
  813. for (j = 0; j < map->len; j++) {
  814. if (map->queues[j] == index) {
  815. cpumask_set_cpu(i, mask);
  816. break;
  817. }
  818. }
  819. }
  820. }
  821. }
  822. rcu_read_unlock();
  823. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  824. if (PAGE_SIZE - len < 3) {
  825. free_cpumask_var(mask);
  826. return -EINVAL;
  827. }
  828. free_cpumask_var(mask);
  829. len += sprintf(buf + len, "\n");
  830. return len;
  831. }
  832. static ssize_t store_xps_map(struct netdev_queue *queue,
  833. struct netdev_queue_attribute *attribute,
  834. const char *buf, size_t len)
  835. {
  836. struct net_device *dev = queue->dev;
  837. unsigned long index;
  838. cpumask_var_t mask;
  839. int err;
  840. if (!capable(CAP_NET_ADMIN))
  841. return -EPERM;
  842. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  843. return -ENOMEM;
  844. index = get_netdev_queue_index(queue);
  845. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  846. if (err) {
  847. free_cpumask_var(mask);
  848. return err;
  849. }
  850. err = netif_set_xps_queue(dev, mask, index);
  851. free_cpumask_var(mask);
  852. return err ? : len;
  853. }
  854. static struct netdev_queue_attribute xps_cpus_attribute =
  855. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  856. #endif /* CONFIG_XPS */
  857. static struct attribute *netdev_queue_default_attrs[] = {
  858. &queue_trans_timeout.attr,
  859. #ifdef CONFIG_XPS
  860. &xps_cpus_attribute.attr,
  861. #endif
  862. NULL
  863. };
  864. static void netdev_queue_release(struct kobject *kobj)
  865. {
  866. struct netdev_queue *queue = to_netdev_queue(kobj);
  867. memset(kobj, 0, sizeof(*kobj));
  868. dev_put(queue->dev);
  869. }
  870. static struct kobj_type netdev_queue_ktype = {
  871. .sysfs_ops = &netdev_queue_sysfs_ops,
  872. .release = netdev_queue_release,
  873. .default_attrs = netdev_queue_default_attrs,
  874. };
  875. static int netdev_queue_add_kobject(struct net_device *net, int index)
  876. {
  877. struct netdev_queue *queue = net->_tx + index;
  878. struct kobject *kobj = &queue->kobj;
  879. int error = 0;
  880. kobj->kset = net->queues_kset;
  881. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  882. "tx-%u", index);
  883. if (error)
  884. goto exit;
  885. #ifdef CONFIG_BQL
  886. error = sysfs_create_group(kobj, &dql_group);
  887. if (error)
  888. goto exit;
  889. #endif
  890. kobject_uevent(kobj, KOBJ_ADD);
  891. dev_hold(queue->dev);
  892. return 0;
  893. exit:
  894. kobject_put(kobj);
  895. return error;
  896. }
  897. #endif /* CONFIG_SYSFS */
  898. int
  899. netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  900. {
  901. #ifdef CONFIG_SYSFS
  902. int i;
  903. int error = 0;
  904. for (i = old_num; i < new_num; i++) {
  905. error = netdev_queue_add_kobject(net, i);
  906. if (error) {
  907. new_num = old_num;
  908. break;
  909. }
  910. }
  911. while (--i >= new_num) {
  912. struct netdev_queue *queue = net->_tx + i;
  913. #ifdef CONFIG_BQL
  914. sysfs_remove_group(&queue->kobj, &dql_group);
  915. #endif
  916. kobject_put(&queue->kobj);
  917. }
  918. return error;
  919. #else
  920. return 0;
  921. #endif /* CONFIG_SYSFS */
  922. }
  923. static int register_queue_kobjects(struct net_device *net)
  924. {
  925. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  926. #ifdef CONFIG_SYSFS
  927. net->queues_kset = kset_create_and_add("queues",
  928. NULL, &net->dev.kobj);
  929. if (!net->queues_kset)
  930. return -ENOMEM;
  931. #endif
  932. #ifdef CONFIG_RPS
  933. real_rx = net->real_num_rx_queues;
  934. #endif
  935. real_tx = net->real_num_tx_queues;
  936. error = net_rx_queue_update_kobjects(net, 0, real_rx);
  937. if (error)
  938. goto error;
  939. rxq = real_rx;
  940. error = netdev_queue_update_kobjects(net, 0, real_tx);
  941. if (error)
  942. goto error;
  943. txq = real_tx;
  944. return 0;
  945. error:
  946. netdev_queue_update_kobjects(net, txq, 0);
  947. net_rx_queue_update_kobjects(net, rxq, 0);
  948. return error;
  949. }
  950. static void remove_queue_kobjects(struct net_device *net)
  951. {
  952. int real_rx = 0, real_tx = 0;
  953. #ifdef CONFIG_RPS
  954. real_rx = net->real_num_rx_queues;
  955. #endif
  956. real_tx = net->real_num_tx_queues;
  957. net_rx_queue_update_kobjects(net, real_rx, 0);
  958. netdev_queue_update_kobjects(net, real_tx, 0);
  959. #ifdef CONFIG_SYSFS
  960. kset_unregister(net->queues_kset);
  961. #endif
  962. }
  963. static void *net_grab_current_ns(void)
  964. {
  965. struct net *ns = current->nsproxy->net_ns;
  966. #ifdef CONFIG_NET_NS
  967. if (ns)
  968. atomic_inc(&ns->passive);
  969. #endif
  970. return ns;
  971. }
  972. static const void *net_initial_ns(void)
  973. {
  974. return &init_net;
  975. }
  976. static const void *net_netlink_ns(struct sock *sk)
  977. {
  978. return sock_net(sk);
  979. }
  980. struct kobj_ns_type_operations net_ns_type_operations = {
  981. .type = KOBJ_NS_TYPE_NET,
  982. .grab_current_ns = net_grab_current_ns,
  983. .netlink_ns = net_netlink_ns,
  984. .initial_ns = net_initial_ns,
  985. .drop_ns = net_drop_ns,
  986. };
  987. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  988. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  989. {
  990. struct net_device *dev = to_net_dev(d);
  991. int retval;
  992. /* pass interface to uevent. */
  993. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  994. if (retval)
  995. goto exit;
  996. /* pass ifindex to uevent.
  997. * ifindex is useful as it won't change (interface name may change)
  998. * and is what RtNetlink uses natively. */
  999. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1000. exit:
  1001. return retval;
  1002. }
  1003. /*
  1004. * netdev_release -- destroy and free a dead device.
  1005. * Called when last reference to device kobject is gone.
  1006. */
  1007. static void netdev_release(struct device *d)
  1008. {
  1009. struct net_device *dev = to_net_dev(d);
  1010. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1011. kfree(dev->ifalias);
  1012. kfree((char *)dev - dev->padded);
  1013. }
  1014. static const void *net_namespace(struct device *d)
  1015. {
  1016. struct net_device *dev;
  1017. dev = container_of(d, struct net_device, dev);
  1018. return dev_net(dev);
  1019. }
  1020. static struct class net_class = {
  1021. .name = "net",
  1022. .dev_release = netdev_release,
  1023. #ifdef CONFIG_SYSFS
  1024. .dev_attrs = net_class_attributes,
  1025. #endif /* CONFIG_SYSFS */
  1026. .dev_uevent = netdev_uevent,
  1027. .ns_type = &net_ns_type_operations,
  1028. .namespace = net_namespace,
  1029. };
  1030. /* Delete sysfs entries but hold kobject reference until after all
  1031. * netdev references are gone.
  1032. */
  1033. void netdev_unregister_kobject(struct net_device * net)
  1034. {
  1035. struct device *dev = &(net->dev);
  1036. kobject_get(&dev->kobj);
  1037. remove_queue_kobjects(net);
  1038. pm_runtime_set_memalloc_noio(dev, false);
  1039. device_del(dev);
  1040. }
  1041. /* Create sysfs entries for network device. */
  1042. int netdev_register_kobject(struct net_device *net)
  1043. {
  1044. struct device *dev = &(net->dev);
  1045. const struct attribute_group **groups = net->sysfs_groups;
  1046. int error = 0;
  1047. device_initialize(dev);
  1048. dev->class = &net_class;
  1049. dev->platform_data = net;
  1050. dev->groups = groups;
  1051. dev_set_name(dev, "%s", net->name);
  1052. #ifdef CONFIG_SYSFS
  1053. /* Allow for a device specific group */
  1054. if (*groups)
  1055. groups++;
  1056. *groups++ = &netstat_group;
  1057. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  1058. if (net->ieee80211_ptr)
  1059. *groups++ = &wireless_group;
  1060. #if IS_ENABLED(CONFIG_WIRELESS_EXT)
  1061. else if (net->wireless_handlers)
  1062. *groups++ = &wireless_group;
  1063. #endif
  1064. #endif
  1065. #endif /* CONFIG_SYSFS */
  1066. error = device_add(dev);
  1067. if (error)
  1068. return error;
  1069. error = register_queue_kobjects(net);
  1070. if (error) {
  1071. device_del(dev);
  1072. return error;
  1073. }
  1074. pm_runtime_set_memalloc_noio(dev, true);
  1075. return error;
  1076. }
  1077. int netdev_class_create_file(struct class_attribute *class_attr)
  1078. {
  1079. return class_create_file(&net_class, class_attr);
  1080. }
  1081. EXPORT_SYMBOL(netdev_class_create_file);
  1082. void netdev_class_remove_file(struct class_attribute *class_attr)
  1083. {
  1084. class_remove_file(&net_class, class_attr);
  1085. }
  1086. EXPORT_SYMBOL(netdev_class_remove_file);
  1087. int netdev_kobject_init(void)
  1088. {
  1089. kobj_ns_type_register(&net_ns_type_operations);
  1090. return class_register(&net_class);
  1091. }