net-sysfs.c 31 KB

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