net-sysfs.c 22 KB

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