PageRenderTime 25ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/net/core/net-sysfs.c

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