net-sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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_ulong[] = "%lu\n";
  29. static inline int dev_isalive(const struct net_device *dev)
  30. {
  31. return dev->reg_state <= NETREG_REGISTERED;
  32. }
  33. /* use same locking rules as GIF* ioctl's */
  34. static ssize_t netdev_show(const struct device *dev,
  35. struct device_attribute *attr, char *buf,
  36. ssize_t (*format)(const struct net_device *, char *))
  37. {
  38. struct net_device *net = to_net_dev(dev);
  39. ssize_t ret = -EINVAL;
  40. read_lock(&dev_base_lock);
  41. if (dev_isalive(net))
  42. ret = (*format)(net, buf);
  43. read_unlock(&dev_base_lock);
  44. return ret;
  45. }
  46. /* generate a show function for simple field */
  47. #define NETDEVICE_SHOW(field, format_string) \
  48. static ssize_t format_##field(const struct net_device *net, char *buf) \
  49. { \
  50. return sprintf(buf, format_string, net->field); \
  51. } \
  52. static ssize_t show_##field(struct device *dev, \
  53. struct device_attribute *attr, char *buf) \
  54. { \
  55. return netdev_show(dev, attr, buf, format_##field); \
  56. }
  57. /* use same locking and permission rules as SIF* ioctl's */
  58. static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  59. const char *buf, size_t len,
  60. int (*set)(struct net_device *, unsigned long))
  61. {
  62. struct net_device *net = to_net_dev(dev);
  63. char *endp;
  64. unsigned long new;
  65. int ret = -EINVAL;
  66. if (!capable(CAP_NET_ADMIN))
  67. return -EPERM;
  68. new = simple_strtoul(buf, &endp, 0);
  69. if (endp == buf)
  70. goto err;
  71. if (!rtnl_trylock())
  72. return restart_syscall();
  73. if (dev_isalive(net)) {
  74. if ((ret = (*set)(net, new)) == 0)
  75. ret = len;
  76. }
  77. rtnl_unlock();
  78. err:
  79. return ret;
  80. }
  81. NETDEVICE_SHOW(dev_id, fmt_hex);
  82. NETDEVICE_SHOW(addr_len, fmt_dec);
  83. NETDEVICE_SHOW(iflink, fmt_dec);
  84. NETDEVICE_SHOW(ifindex, fmt_dec);
  85. NETDEVICE_SHOW(features, fmt_long_hex);
  86. NETDEVICE_SHOW(type, fmt_dec);
  87. NETDEVICE_SHOW(link_mode, fmt_dec);
  88. /* use same locking rules as GIFHWADDR ioctl's */
  89. static ssize_t show_address(struct device *dev, struct device_attribute *attr,
  90. char *buf)
  91. {
  92. struct net_device *net = to_net_dev(dev);
  93. ssize_t ret = -EINVAL;
  94. read_lock(&dev_base_lock);
  95. if (dev_isalive(net))
  96. ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
  97. read_unlock(&dev_base_lock);
  98. return ret;
  99. }
  100. static ssize_t show_broadcast(struct device *dev,
  101. struct device_attribute *attr, char *buf)
  102. {
  103. struct net_device *net = to_net_dev(dev);
  104. if (dev_isalive(net))
  105. return sysfs_format_mac(buf, net->broadcast, net->addr_len);
  106. return -EINVAL;
  107. }
  108. static ssize_t show_carrier(struct device *dev,
  109. struct device_attribute *attr, char *buf)
  110. {
  111. struct net_device *netdev = to_net_dev(dev);
  112. if (netif_running(netdev)) {
  113. return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
  114. }
  115. return -EINVAL;
  116. }
  117. static ssize_t show_speed(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct net_device *netdev = to_net_dev(dev);
  121. int ret = -EINVAL;
  122. if (!rtnl_trylock())
  123. return restart_syscall();
  124. if (netif_running(netdev) &&
  125. netdev->ethtool_ops &&
  126. netdev->ethtool_ops->get_settings) {
  127. struct ethtool_cmd cmd = { ETHTOOL_GSET };
  128. if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
  129. ret = sprintf(buf, fmt_dec, 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. netdev->ethtool_ops &&
  143. netdev->ethtool_ops->get_settings) {
  144. struct ethtool_cmd cmd = { ETHTOOL_GSET };
  145. if (!netdev->ethtool_ops->get_settings(netdev, &cmd))
  146. ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
  147. }
  148. rtnl_unlock();
  149. return ret;
  150. }
  151. static ssize_t show_dormant(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct net_device *netdev = to_net_dev(dev);
  155. if (netif_running(netdev))
  156. return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
  157. return -EINVAL;
  158. }
  159. static const char *const operstates[] = {
  160. "unknown",
  161. "notpresent", /* currently unused */
  162. "down",
  163. "lowerlayerdown",
  164. "testing", /* currently unused */
  165. "dormant",
  166. "up"
  167. };
  168. static ssize_t show_operstate(struct device *dev,
  169. struct device_attribute *attr, char *buf)
  170. {
  171. const struct net_device *netdev = to_net_dev(dev);
  172. unsigned char operstate;
  173. read_lock(&dev_base_lock);
  174. operstate = netdev->operstate;
  175. if (!netif_running(netdev))
  176. operstate = IF_OPER_DOWN;
  177. read_unlock(&dev_base_lock);
  178. if (operstate >= ARRAY_SIZE(operstates))
  179. return -EINVAL; /* should not happen */
  180. return sprintf(buf, "%s\n", operstates[operstate]);
  181. }
  182. /* read-write attributes */
  183. NETDEVICE_SHOW(mtu, fmt_dec);
  184. static int change_mtu(struct net_device *net, unsigned long new_mtu)
  185. {
  186. return dev_set_mtu(net, (int) new_mtu);
  187. }
  188. static ssize_t store_mtu(struct device *dev, struct device_attribute *attr,
  189. const char *buf, size_t len)
  190. {
  191. return netdev_store(dev, attr, buf, len, change_mtu);
  192. }
  193. NETDEVICE_SHOW(flags, fmt_hex);
  194. static int change_flags(struct net_device *net, unsigned long new_flags)
  195. {
  196. return dev_change_flags(net, (unsigned) new_flags);
  197. }
  198. static ssize_t store_flags(struct device *dev, struct device_attribute *attr,
  199. const char *buf, size_t len)
  200. {
  201. return netdev_store(dev, attr, buf, len, change_flags);
  202. }
  203. NETDEVICE_SHOW(tx_queue_len, fmt_ulong);
  204. static int change_tx_queue_len(struct net_device *net, unsigned long new_len)
  205. {
  206. net->tx_queue_len = new_len;
  207. return 0;
  208. }
  209. static ssize_t store_tx_queue_len(struct device *dev,
  210. struct device_attribute *attr,
  211. const char *buf, size_t len)
  212. {
  213. return netdev_store(dev, attr, buf, len, change_tx_queue_len);
  214. }
  215. static ssize_t store_ifalias(struct device *dev, struct device_attribute *attr,
  216. const char *buf, size_t len)
  217. {
  218. struct net_device *netdev = to_net_dev(dev);
  219. size_t count = len;
  220. ssize_t ret;
  221. if (!capable(CAP_NET_ADMIN))
  222. return -EPERM;
  223. /* ignore trailing newline */
  224. if (len > 0 && buf[len - 1] == '\n')
  225. --count;
  226. if (!rtnl_trylock())
  227. return restart_syscall();
  228. ret = dev_set_alias(netdev, buf, count);
  229. rtnl_unlock();
  230. return ret < 0 ? ret : len;
  231. }
  232. static ssize_t show_ifalias(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. const struct net_device *netdev = to_net_dev(dev);
  236. ssize_t ret = 0;
  237. if (!rtnl_trylock())
  238. return restart_syscall();
  239. if (netdev->ifalias)
  240. ret = sprintf(buf, "%s\n", netdev->ifalias);
  241. rtnl_unlock();
  242. return ret;
  243. }
  244. static struct device_attribute net_class_attributes[] = {
  245. __ATTR(addr_len, S_IRUGO, show_addr_len, NULL),
  246. __ATTR(dev_id, S_IRUGO, show_dev_id, NULL),
  247. __ATTR(ifalias, S_IRUGO | S_IWUSR, show_ifalias, store_ifalias),
  248. __ATTR(iflink, S_IRUGO, show_iflink, NULL),
  249. __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
  250. __ATTR(features, S_IRUGO, show_features, NULL),
  251. __ATTR(type, S_IRUGO, show_type, NULL),
  252. __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
  253. __ATTR(address, S_IRUGO, show_address, NULL),
  254. __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
  255. __ATTR(carrier, S_IRUGO, show_carrier, NULL),
  256. __ATTR(speed, S_IRUGO, show_speed, NULL),
  257. __ATTR(duplex, S_IRUGO, show_duplex, NULL),
  258. __ATTR(dormant, S_IRUGO, show_dormant, NULL),
  259. __ATTR(operstate, S_IRUGO, show_operstate, NULL),
  260. __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
  261. __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
  262. __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
  263. store_tx_queue_len),
  264. {}
  265. };
  266. /* Show a given an attribute in the statistics group */
  267. static ssize_t netstat_show(const struct device *d,
  268. struct device_attribute *attr, char *buf,
  269. unsigned long offset)
  270. {
  271. struct net_device *dev = to_net_dev(d);
  272. ssize_t ret = -EINVAL;
  273. WARN_ON(offset > sizeof(struct net_device_stats) ||
  274. offset % sizeof(unsigned long) != 0);
  275. read_lock(&dev_base_lock);
  276. if (dev_isalive(dev)) {
  277. const struct net_device_stats *stats = dev_get_stats(dev);
  278. ret = sprintf(buf, fmt_ulong,
  279. *(unsigned long *)(((u8 *) stats) + offset));
  280. }
  281. read_unlock(&dev_base_lock);
  282. return ret;
  283. }
  284. /* generate a read-only statistics attribute */
  285. #define NETSTAT_ENTRY(name) \
  286. static ssize_t show_##name(struct device *d, \
  287. struct device_attribute *attr, char *buf) \
  288. { \
  289. return netstat_show(d, attr, buf, \
  290. offsetof(struct net_device_stats, name)); \
  291. } \
  292. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  293. NETSTAT_ENTRY(rx_packets);
  294. NETSTAT_ENTRY(tx_packets);
  295. NETSTAT_ENTRY(rx_bytes);
  296. NETSTAT_ENTRY(tx_bytes);
  297. NETSTAT_ENTRY(rx_errors);
  298. NETSTAT_ENTRY(tx_errors);
  299. NETSTAT_ENTRY(rx_dropped);
  300. NETSTAT_ENTRY(tx_dropped);
  301. NETSTAT_ENTRY(multicast);
  302. NETSTAT_ENTRY(collisions);
  303. NETSTAT_ENTRY(rx_length_errors);
  304. NETSTAT_ENTRY(rx_over_errors);
  305. NETSTAT_ENTRY(rx_crc_errors);
  306. NETSTAT_ENTRY(rx_frame_errors);
  307. NETSTAT_ENTRY(rx_fifo_errors);
  308. NETSTAT_ENTRY(rx_missed_errors);
  309. NETSTAT_ENTRY(tx_aborted_errors);
  310. NETSTAT_ENTRY(tx_carrier_errors);
  311. NETSTAT_ENTRY(tx_fifo_errors);
  312. NETSTAT_ENTRY(tx_heartbeat_errors);
  313. NETSTAT_ENTRY(tx_window_errors);
  314. NETSTAT_ENTRY(rx_compressed);
  315. NETSTAT_ENTRY(tx_compressed);
  316. static struct attribute *netstat_attrs[] = {
  317. &dev_attr_rx_packets.attr,
  318. &dev_attr_tx_packets.attr,
  319. &dev_attr_rx_bytes.attr,
  320. &dev_attr_tx_bytes.attr,
  321. &dev_attr_rx_errors.attr,
  322. &dev_attr_tx_errors.attr,
  323. &dev_attr_rx_dropped.attr,
  324. &dev_attr_tx_dropped.attr,
  325. &dev_attr_multicast.attr,
  326. &dev_attr_collisions.attr,
  327. &dev_attr_rx_length_errors.attr,
  328. &dev_attr_rx_over_errors.attr,
  329. &dev_attr_rx_crc_errors.attr,
  330. &dev_attr_rx_frame_errors.attr,
  331. &dev_attr_rx_fifo_errors.attr,
  332. &dev_attr_rx_missed_errors.attr,
  333. &dev_attr_tx_aborted_errors.attr,
  334. &dev_attr_tx_carrier_errors.attr,
  335. &dev_attr_tx_fifo_errors.attr,
  336. &dev_attr_tx_heartbeat_errors.attr,
  337. &dev_attr_tx_window_errors.attr,
  338. &dev_attr_rx_compressed.attr,
  339. &dev_attr_tx_compressed.attr,
  340. NULL
  341. };
  342. static struct attribute_group netstat_group = {
  343. .name = "statistics",
  344. .attrs = netstat_attrs,
  345. };
  346. #ifdef CONFIG_WIRELESS_EXT_SYSFS
  347. /* helper function that does all the locking etc for wireless stats */
  348. static ssize_t wireless_show(struct device *d, char *buf,
  349. ssize_t (*format)(const struct iw_statistics *,
  350. char *))
  351. {
  352. struct net_device *dev = to_net_dev(d);
  353. const struct iw_statistics *iw;
  354. ssize_t ret = -EINVAL;
  355. if (!rtnl_trylock())
  356. return restart_syscall();
  357. if (dev_isalive(dev)) {
  358. iw = get_wireless_stats(dev);
  359. if (iw)
  360. ret = (*format)(iw, buf);
  361. }
  362. rtnl_unlock();
  363. return ret;
  364. }
  365. /* show function template for wireless fields */
  366. #define WIRELESS_SHOW(name, field, format_string) \
  367. static ssize_t format_iw_##name(const struct iw_statistics *iw, char *buf) \
  368. { \
  369. return sprintf(buf, format_string, iw->field); \
  370. } \
  371. static ssize_t show_iw_##name(struct device *d, \
  372. struct device_attribute *attr, char *buf) \
  373. { \
  374. return wireless_show(d, buf, format_iw_##name); \
  375. } \
  376. static DEVICE_ATTR(name, S_IRUGO, show_iw_##name, NULL)
  377. WIRELESS_SHOW(status, status, fmt_hex);
  378. WIRELESS_SHOW(link, qual.qual, fmt_dec);
  379. WIRELESS_SHOW(level, qual.level, fmt_dec);
  380. WIRELESS_SHOW(noise, qual.noise, fmt_dec);
  381. WIRELESS_SHOW(nwid, discard.nwid, fmt_dec);
  382. WIRELESS_SHOW(crypt, discard.code, fmt_dec);
  383. WIRELESS_SHOW(fragment, discard.fragment, fmt_dec);
  384. WIRELESS_SHOW(misc, discard.misc, fmt_dec);
  385. WIRELESS_SHOW(retries, discard.retries, fmt_dec);
  386. WIRELESS_SHOW(beacon, miss.beacon, fmt_dec);
  387. static struct attribute *wireless_attrs[] = {
  388. &dev_attr_status.attr,
  389. &dev_attr_link.attr,
  390. &dev_attr_level.attr,
  391. &dev_attr_noise.attr,
  392. &dev_attr_nwid.attr,
  393. &dev_attr_crypt.attr,
  394. &dev_attr_fragment.attr,
  395. &dev_attr_retries.attr,
  396. &dev_attr_misc.attr,
  397. &dev_attr_beacon.attr,
  398. NULL
  399. };
  400. static struct attribute_group wireless_group = {
  401. .name = "wireless",
  402. .attrs = wireless_attrs,
  403. };
  404. #endif
  405. #ifdef CONFIG_RPS
  406. /*
  407. * RX queue sysfs structures and functions.
  408. */
  409. struct rx_queue_attribute {
  410. struct attribute attr;
  411. ssize_t (*show)(struct netdev_rx_queue *queue,
  412. struct rx_queue_attribute *attr, char *buf);
  413. ssize_t (*store)(struct netdev_rx_queue *queue,
  414. struct rx_queue_attribute *attr, const char *buf, size_t len);
  415. };
  416. #define to_rx_queue_attr(_attr) container_of(_attr, \
  417. struct rx_queue_attribute, attr)
  418. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  419. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  420. char *buf)
  421. {
  422. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  423. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  424. if (!attribute->show)
  425. return -EIO;
  426. return attribute->show(queue, attribute, buf);
  427. }
  428. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  429. const char *buf, size_t count)
  430. {
  431. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  432. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  433. if (!attribute->store)
  434. return -EIO;
  435. return attribute->store(queue, attribute, buf, count);
  436. }
  437. static struct sysfs_ops rx_queue_sysfs_ops = {
  438. .show = rx_queue_attr_show,
  439. .store = rx_queue_attr_store,
  440. };
  441. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  442. struct rx_queue_attribute *attribute, char *buf)
  443. {
  444. struct rps_map *map;
  445. cpumask_var_t mask;
  446. size_t len = 0;
  447. int i;
  448. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  449. return -ENOMEM;
  450. rcu_read_lock();
  451. map = rcu_dereference(queue->rps_map);
  452. if (map)
  453. for (i = 0; i < map->len; i++)
  454. cpumask_set_cpu(map->cpus[i], mask);
  455. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  456. if (PAGE_SIZE - len < 3) {
  457. rcu_read_unlock();
  458. free_cpumask_var(mask);
  459. return -EINVAL;
  460. }
  461. rcu_read_unlock();
  462. free_cpumask_var(mask);
  463. len += sprintf(buf + len, "\n");
  464. return len;
  465. }
  466. static void rps_map_release(struct rcu_head *rcu)
  467. {
  468. struct rps_map *map = container_of(rcu, struct rps_map, rcu);
  469. kfree(map);
  470. }
  471. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  472. struct rx_queue_attribute *attribute,
  473. const char *buf, size_t len)
  474. {
  475. struct rps_map *old_map, *map;
  476. cpumask_var_t mask;
  477. int err, cpu, i;
  478. static DEFINE_SPINLOCK(rps_map_lock);
  479. if (!capable(CAP_NET_ADMIN))
  480. return -EPERM;
  481. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  482. return -ENOMEM;
  483. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  484. if (err) {
  485. free_cpumask_var(mask);
  486. return err;
  487. }
  488. map = kzalloc(max_t(unsigned,
  489. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  490. GFP_KERNEL);
  491. if (!map) {
  492. free_cpumask_var(mask);
  493. return -ENOMEM;
  494. }
  495. i = 0;
  496. for_each_cpu_and(cpu, mask, cpu_online_mask)
  497. map->cpus[i++] = cpu;
  498. if (i)
  499. map->len = i;
  500. else {
  501. kfree(map);
  502. map = NULL;
  503. }
  504. spin_lock(&rps_map_lock);
  505. old_map = queue->rps_map;
  506. rcu_assign_pointer(queue->rps_map, map);
  507. spin_unlock(&rps_map_lock);
  508. if (old_map)
  509. call_rcu(&old_map->rcu, rps_map_release);
  510. free_cpumask_var(mask);
  511. return len;
  512. }
  513. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  514. struct rx_queue_attribute *attr,
  515. char *buf)
  516. {
  517. struct rps_dev_flow_table *flow_table;
  518. unsigned int val = 0;
  519. rcu_read_lock();
  520. flow_table = rcu_dereference(queue->rps_flow_table);
  521. if (flow_table)
  522. val = flow_table->mask + 1;
  523. rcu_read_unlock();
  524. return sprintf(buf, "%u\n", val);
  525. }
  526. static void rps_dev_flow_table_release_work(struct work_struct *work)
  527. {
  528. struct rps_dev_flow_table *table = container_of(work,
  529. struct rps_dev_flow_table, free_work);
  530. vfree(table);
  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. INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
  537. schedule_work(&table->free_work);
  538. }
  539. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  540. struct rx_queue_attribute *attr,
  541. const char *buf, size_t len)
  542. {
  543. unsigned int count;
  544. char *endp;
  545. struct rps_dev_flow_table *table, *old_table;
  546. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  547. if (!capable(CAP_NET_ADMIN))
  548. return -EPERM;
  549. count = simple_strtoul(buf, &endp, 0);
  550. if (endp == buf)
  551. return -EINVAL;
  552. if (count) {
  553. int i;
  554. if (count > 1<<30) {
  555. /* Enforce a limit to prevent overflow */
  556. return -EINVAL;
  557. }
  558. count = roundup_pow_of_two(count);
  559. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
  560. if (!table)
  561. return -ENOMEM;
  562. table->mask = count - 1;
  563. for (i = 0; i < count; i++)
  564. table->flows[i].cpu = RPS_NO_CPU;
  565. } else
  566. table = NULL;
  567. spin_lock(&rps_dev_flow_lock);
  568. old_table = queue->rps_flow_table;
  569. rcu_assign_pointer(queue->rps_flow_table, table);
  570. spin_unlock(&rps_dev_flow_lock);
  571. if (old_table)
  572. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  573. return len;
  574. }
  575. static struct rx_queue_attribute rps_cpus_attribute =
  576. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  577. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  578. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  579. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  580. static struct attribute *rx_queue_default_attrs[] = {
  581. &rps_cpus_attribute.attr,
  582. &rps_dev_flow_table_cnt_attribute.attr,
  583. NULL
  584. };
  585. static void rx_queue_release(struct kobject *kobj)
  586. {
  587. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  588. struct netdev_rx_queue *first = queue->first;
  589. if (queue->rps_map)
  590. call_rcu(&queue->rps_map->rcu, rps_map_release);
  591. if (queue->rps_flow_table)
  592. call_rcu(&queue->rps_flow_table->rcu,
  593. rps_dev_flow_table_release);
  594. if (atomic_dec_and_test(&first->count))
  595. kfree(first);
  596. }
  597. static struct kobj_type rx_queue_ktype = {
  598. .sysfs_ops = &rx_queue_sysfs_ops,
  599. .release = rx_queue_release,
  600. .default_attrs = rx_queue_default_attrs,
  601. };
  602. static int rx_queue_add_kobject(struct net_device *net, int index)
  603. {
  604. struct netdev_rx_queue *queue = net->_rx + index;
  605. struct kobject *kobj = &queue->kobj;
  606. int error = 0;
  607. kobj->kset = net->queues_kset;
  608. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  609. "rx-%u", index);
  610. if (error) {
  611. kobject_put(kobj);
  612. return error;
  613. }
  614. kobject_uevent(kobj, KOBJ_ADD);
  615. return error;
  616. }
  617. static int rx_queue_register_kobjects(struct net_device *net)
  618. {
  619. int i;
  620. int error = 0;
  621. net->queues_kset = kset_create_and_add("queues",
  622. NULL, &net->dev.kobj);
  623. if (!net->queues_kset)
  624. return -ENOMEM;
  625. for (i = 0; i < net->num_rx_queues; i++) {
  626. error = rx_queue_add_kobject(net, i);
  627. if (error)
  628. break;
  629. }
  630. if (error)
  631. while (--i >= 0)
  632. kobject_put(&net->_rx[i].kobj);
  633. return error;
  634. }
  635. static void rx_queue_remove_kobjects(struct net_device *net)
  636. {
  637. int i;
  638. for (i = 0; i < net->num_rx_queues; i++)
  639. kobject_put(&net->_rx[i].kobj);
  640. kset_unregister(net->queues_kset);
  641. }
  642. #endif /* CONFIG_RPS */
  643. static const void *net_current_ns(void)
  644. {
  645. return current->nsproxy->net_ns;
  646. }
  647. static const void *net_initial_ns(void)
  648. {
  649. return &init_net;
  650. }
  651. static const void *net_netlink_ns(struct sock *sk)
  652. {
  653. return sock_net(sk);
  654. }
  655. static struct kobj_ns_type_operations net_ns_type_operations = {
  656. .type = KOBJ_NS_TYPE_NET,
  657. .current_ns = net_current_ns,
  658. .netlink_ns = net_netlink_ns,
  659. .initial_ns = net_initial_ns,
  660. };
  661. static void net_kobj_ns_exit(struct net *net)
  662. {
  663. kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
  664. }
  665. static struct pernet_operations sysfs_net_ops = {
  666. .exit = net_kobj_ns_exit,
  667. };
  668. #endif /* CONFIG_SYSFS */
  669. #ifdef CONFIG_HOTPLUG
  670. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  671. {
  672. struct net_device *dev = to_net_dev(d);
  673. int retval;
  674. if (!net_eq(dev_net(dev), &init_net))
  675. return 0;
  676. /* pass interface to uevent. */
  677. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  678. if (retval)
  679. goto exit;
  680. /* pass ifindex to uevent.
  681. * ifindex is useful as it won't change (interface name may change)
  682. * and is what RtNetlink uses natively. */
  683. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  684. exit:
  685. return retval;
  686. }
  687. #endif
  688. /*
  689. * netdev_release -- destroy and free a dead device.
  690. * Called when last reference to device kobject is gone.
  691. */
  692. static void netdev_release(struct device *d)
  693. {
  694. struct net_device *dev = to_net_dev(d);
  695. BUG_ON(dev->reg_state != NETREG_RELEASED);
  696. kfree(dev->ifalias);
  697. kfree((char *)dev - dev->padded);
  698. }
  699. static const void *net_namespace(struct device *d)
  700. {
  701. struct net_device *dev;
  702. dev = container_of(d, struct net_device, dev);
  703. return dev_net(dev);
  704. }
  705. static struct class net_class = {
  706. .name = "net",
  707. .dev_release = netdev_release,
  708. #ifdef CONFIG_SYSFS
  709. .dev_attrs = net_class_attributes,
  710. #endif /* CONFIG_SYSFS */
  711. #ifdef CONFIG_HOTPLUG
  712. .dev_uevent = netdev_uevent,
  713. #endif
  714. .ns_type = &net_ns_type_operations,
  715. .namespace = net_namespace,
  716. };
  717. /* Delete sysfs entries but hold kobject reference until after all
  718. * netdev references are gone.
  719. */
  720. void netdev_unregister_kobject(struct net_device * net)
  721. {
  722. struct device *dev = &(net->dev);
  723. kobject_get(&dev->kobj);
  724. if (!net_eq(dev_net(net), &init_net))
  725. return;
  726. #ifdef CONFIG_RPS
  727. rx_queue_remove_kobjects(net);
  728. #endif
  729. device_del(dev);
  730. }
  731. /* Create sysfs entries for network device. */
  732. int netdev_register_kobject(struct net_device *net)
  733. {
  734. struct device *dev = &(net->dev);
  735. const struct attribute_group **groups = net->sysfs_groups;
  736. int error = 0;
  737. dev->class = &net_class;
  738. dev->platform_data = net;
  739. dev->groups = groups;
  740. dev_set_name(dev, "%s", net->name);
  741. #ifdef CONFIG_SYSFS
  742. /* Allow for a device specific group */
  743. if (*groups)
  744. groups++;
  745. *groups++ = &netstat_group;
  746. #ifdef CONFIG_WIRELESS_EXT_SYSFS
  747. if (net->ieee80211_ptr)
  748. *groups++ = &wireless_group;
  749. #ifdef CONFIG_WIRELESS_EXT
  750. else if (net->wireless_handlers)
  751. *groups++ = &wireless_group;
  752. #endif
  753. #endif
  754. #endif /* CONFIG_SYSFS */
  755. if (!net_eq(dev_net(net), &init_net))
  756. return 0;
  757. error = device_add(dev);
  758. if (error)
  759. return error;
  760. #ifdef CONFIG_RPS
  761. error = rx_queue_register_kobjects(net);
  762. if (error) {
  763. device_del(dev);
  764. return error;
  765. }
  766. #endif
  767. return error;
  768. }
  769. int netdev_class_create_file(struct class_attribute *class_attr)
  770. {
  771. return class_create_file(&net_class, class_attr);
  772. }
  773. void netdev_class_remove_file(struct class_attribute *class_attr)
  774. {
  775. class_remove_file(&net_class, class_attr);
  776. }
  777. EXPORT_SYMBOL(netdev_class_create_file);
  778. EXPORT_SYMBOL(netdev_class_remove_file);
  779. void netdev_initialize_kobject(struct net_device *net)
  780. {
  781. struct device *device = &(net->dev);
  782. device_initialize(device);
  783. }
  784. int netdev_kobject_init(void)
  785. {
  786. kobj_ns_type_register(&net_ns_type_operations);
  787. #ifdef CONFIG_SYSFS
  788. register_pernet_subsys(&sysfs_net_ops);
  789. #endif
  790. return class_register(&net_class);
  791. }