net-sysfs.c 31 KB

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