net-sysfs.c 36 KB

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