net-sysfs.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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) 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,
  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. jump_label_inc(&rps_needed);
  520. if (old_map) {
  521. kfree_rcu(old_map, rcu);
  522. jump_label_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 int val = 0;
  533. rcu_read_lock();
  534. flow_table = rcu_dereference(queue->rps_flow_table);
  535. if (flow_table)
  536. val = flow_table->mask + 1;
  537. rcu_read_unlock();
  538. return sprintf(buf, "%u\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 int count;
  558. char *endp;
  559. struct rps_dev_flow_table *table, *old_table;
  560. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  561. if (!capable(CAP_NET_ADMIN))
  562. return -EPERM;
  563. count = simple_strtoul(buf, &endp, 0);
  564. if (endp == buf)
  565. return -EINVAL;
  566. if (count) {
  567. int i;
  568. if (count > 1<<30) {
  569. /* Enforce a limit to prevent overflow */
  570. return -EINVAL;
  571. }
  572. count = roundup_pow_of_two(count);
  573. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
  574. if (!table)
  575. return -ENOMEM;
  576. table->mask = count - 1;
  577. for (i = 0; i < count; i++)
  578. table->flows[i].cpu = RPS_NO_CPU;
  579. } else
  580. table = NULL;
  581. spin_lock(&rps_dev_flow_lock);
  582. old_table = rcu_dereference_protected(queue->rps_flow_table,
  583. lockdep_is_held(&rps_dev_flow_lock));
  584. rcu_assign_pointer(queue->rps_flow_table, table);
  585. spin_unlock(&rps_dev_flow_lock);
  586. if (old_table)
  587. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  588. return len;
  589. }
  590. static struct rx_queue_attribute rps_cpus_attribute =
  591. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  592. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  593. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  594. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  595. static struct attribute *rx_queue_default_attrs[] = {
  596. &rps_cpus_attribute.attr,
  597. &rps_dev_flow_table_cnt_attribute.attr,
  598. NULL
  599. };
  600. static void rx_queue_release(struct kobject *kobj)
  601. {
  602. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  603. struct rps_map *map;
  604. struct rps_dev_flow_table *flow_table;
  605. map = rcu_dereference_protected(queue->rps_map, 1);
  606. if (map) {
  607. RCU_INIT_POINTER(queue->rps_map, NULL);
  608. kfree_rcu(map, rcu);
  609. }
  610. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  611. if (flow_table) {
  612. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  613. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  614. }
  615. memset(kobj, 0, sizeof(*kobj));
  616. dev_put(queue->dev);
  617. }
  618. static struct kobj_type rx_queue_ktype = {
  619. .sysfs_ops = &rx_queue_sysfs_ops,
  620. .release = rx_queue_release,
  621. .default_attrs = rx_queue_default_attrs,
  622. };
  623. static int rx_queue_add_kobject(struct net_device *net, int index)
  624. {
  625. struct netdev_rx_queue *queue = net->_rx + index;
  626. struct kobject *kobj = &queue->kobj;
  627. int error = 0;
  628. kobj->kset = net->queues_kset;
  629. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  630. "rx-%u", index);
  631. if (error) {
  632. kobject_put(kobj);
  633. return error;
  634. }
  635. kobject_uevent(kobj, KOBJ_ADD);
  636. dev_hold(queue->dev);
  637. return error;
  638. }
  639. #endif /* CONFIG_RPS */
  640. int
  641. net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  642. {
  643. #ifdef CONFIG_RPS
  644. int i;
  645. int error = 0;
  646. for (i = old_num; i < new_num; i++) {
  647. error = rx_queue_add_kobject(net, i);
  648. if (error) {
  649. new_num = old_num;
  650. break;
  651. }
  652. }
  653. while (--i >= new_num)
  654. kobject_put(&net->_rx[i].kobj);
  655. return error;
  656. #else
  657. return 0;
  658. #endif
  659. }
  660. #ifdef CONFIG_SYSFS
  661. /*
  662. * netdev_queue sysfs structures and functions.
  663. */
  664. struct netdev_queue_attribute {
  665. struct attribute attr;
  666. ssize_t (*show)(struct netdev_queue *queue,
  667. struct netdev_queue_attribute *attr, char *buf);
  668. ssize_t (*store)(struct netdev_queue *queue,
  669. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  670. };
  671. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  672. struct netdev_queue_attribute, attr)
  673. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  674. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  675. struct attribute *attr, char *buf)
  676. {
  677. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  678. struct netdev_queue *queue = to_netdev_queue(kobj);
  679. if (!attribute->show)
  680. return -EIO;
  681. return attribute->show(queue, attribute, buf);
  682. }
  683. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  684. struct attribute *attr,
  685. const char *buf, size_t count)
  686. {
  687. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  688. struct netdev_queue *queue = to_netdev_queue(kobj);
  689. if (!attribute->store)
  690. return -EIO;
  691. return attribute->store(queue, attribute, buf, count);
  692. }
  693. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  694. .show = netdev_queue_attr_show,
  695. .store = netdev_queue_attr_store,
  696. };
  697. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  698. struct netdev_queue_attribute *attribute,
  699. char *buf)
  700. {
  701. unsigned long trans_timeout;
  702. spin_lock_irq(&queue->_xmit_lock);
  703. trans_timeout = queue->trans_timeout;
  704. spin_unlock_irq(&queue->_xmit_lock);
  705. return sprintf(buf, "%lu", trans_timeout);
  706. }
  707. static struct netdev_queue_attribute queue_trans_timeout =
  708. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  709. #ifdef CONFIG_BQL
  710. /*
  711. * Byte queue limits sysfs structures and functions.
  712. */
  713. static ssize_t bql_show(char *buf, unsigned int value)
  714. {
  715. return sprintf(buf, "%u\n", value);
  716. }
  717. static ssize_t bql_set(const char *buf, const size_t count,
  718. unsigned int *pvalue)
  719. {
  720. unsigned int value;
  721. int err;
  722. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  723. value = DQL_MAX_LIMIT;
  724. else {
  725. err = kstrtouint(buf, 10, &value);
  726. if (err < 0)
  727. return err;
  728. if (value > DQL_MAX_LIMIT)
  729. return -EINVAL;
  730. }
  731. *pvalue = value;
  732. return count;
  733. }
  734. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  735. struct netdev_queue_attribute *attr,
  736. char *buf)
  737. {
  738. struct dql *dql = &queue->dql;
  739. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  740. }
  741. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  742. struct netdev_queue_attribute *attribute,
  743. const char *buf, size_t len)
  744. {
  745. struct dql *dql = &queue->dql;
  746. unsigned value;
  747. int err;
  748. err = kstrtouint(buf, 10, &value);
  749. if (err < 0)
  750. return err;
  751. dql->slack_hold_time = msecs_to_jiffies(value);
  752. return len;
  753. }
  754. static struct netdev_queue_attribute bql_hold_time_attribute =
  755. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  756. bql_set_hold_time);
  757. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  758. struct netdev_queue_attribute *attr,
  759. char *buf)
  760. {
  761. struct dql *dql = &queue->dql;
  762. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  763. }
  764. static struct netdev_queue_attribute bql_inflight_attribute =
  765. __ATTR(inflight, S_IRUGO | S_IWUSR, bql_show_inflight, NULL);
  766. #define BQL_ATTR(NAME, FIELD) \
  767. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  768. struct netdev_queue_attribute *attr, \
  769. char *buf) \
  770. { \
  771. return bql_show(buf, queue->dql.FIELD); \
  772. } \
  773. \
  774. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  775. struct netdev_queue_attribute *attr, \
  776. const char *buf, size_t len) \
  777. { \
  778. return bql_set(buf, len, &queue->dql.FIELD); \
  779. } \
  780. \
  781. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  782. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  783. bql_set_ ## NAME);
  784. BQL_ATTR(limit, limit)
  785. BQL_ATTR(limit_max, max_limit)
  786. BQL_ATTR(limit_min, min_limit)
  787. static struct attribute *dql_attrs[] = {
  788. &bql_limit_attribute.attr,
  789. &bql_limit_max_attribute.attr,
  790. &bql_limit_min_attribute.attr,
  791. &bql_hold_time_attribute.attr,
  792. &bql_inflight_attribute.attr,
  793. NULL
  794. };
  795. static struct attribute_group dql_group = {
  796. .name = "byte_queue_limits",
  797. .attrs = dql_attrs,
  798. };
  799. #endif /* CONFIG_BQL */
  800. #ifdef CONFIG_XPS
  801. static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  802. {
  803. struct net_device *dev = queue->dev;
  804. int i;
  805. for (i = 0; i < dev->num_tx_queues; i++)
  806. if (queue == &dev->_tx[i])
  807. break;
  808. BUG_ON(i >= dev->num_tx_queues);
  809. return i;
  810. }
  811. static ssize_t show_xps_map(struct netdev_queue *queue,
  812. struct netdev_queue_attribute *attribute, char *buf)
  813. {
  814. struct net_device *dev = queue->dev;
  815. struct xps_dev_maps *dev_maps;
  816. cpumask_var_t mask;
  817. unsigned long index;
  818. size_t len = 0;
  819. int i;
  820. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  821. return -ENOMEM;
  822. index = get_netdev_queue_index(queue);
  823. rcu_read_lock();
  824. dev_maps = rcu_dereference(dev->xps_maps);
  825. if (dev_maps) {
  826. for_each_possible_cpu(i) {
  827. struct xps_map *map =
  828. rcu_dereference(dev_maps->cpu_map[i]);
  829. if (map) {
  830. int j;
  831. for (j = 0; j < map->len; j++) {
  832. if (map->queues[j] == index) {
  833. cpumask_set_cpu(i, mask);
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. rcu_read_unlock();
  841. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  842. if (PAGE_SIZE - len < 3) {
  843. free_cpumask_var(mask);
  844. return -EINVAL;
  845. }
  846. free_cpumask_var(mask);
  847. len += sprintf(buf + len, "\n");
  848. return len;
  849. }
  850. static DEFINE_MUTEX(xps_map_mutex);
  851. #define xmap_dereference(P) \
  852. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  853. static void xps_queue_release(struct netdev_queue *queue)
  854. {
  855. struct net_device *dev = queue->dev;
  856. struct xps_dev_maps *dev_maps;
  857. struct xps_map *map;
  858. unsigned long index;
  859. int i, pos, nonempty = 0;
  860. index = get_netdev_queue_index(queue);
  861. mutex_lock(&xps_map_mutex);
  862. dev_maps = xmap_dereference(dev->xps_maps);
  863. if (dev_maps) {
  864. for_each_possible_cpu(i) {
  865. map = xmap_dereference(dev_maps->cpu_map[i]);
  866. if (!map)
  867. continue;
  868. for (pos = 0; pos < map->len; pos++)
  869. if (map->queues[pos] == index)
  870. break;
  871. if (pos < map->len) {
  872. if (map->len > 1)
  873. map->queues[pos] =
  874. map->queues[--map->len];
  875. else {
  876. RCU_INIT_POINTER(dev_maps->cpu_map[i],
  877. NULL);
  878. kfree_rcu(map, rcu);
  879. map = NULL;
  880. }
  881. }
  882. if (map)
  883. nonempty = 1;
  884. }
  885. if (!nonempty) {
  886. RCU_INIT_POINTER(dev->xps_maps, NULL);
  887. kfree_rcu(dev_maps, rcu);
  888. }
  889. }
  890. mutex_unlock(&xps_map_mutex);
  891. }
  892. static ssize_t store_xps_map(struct netdev_queue *queue,
  893. struct netdev_queue_attribute *attribute,
  894. const char *buf, size_t len)
  895. {
  896. struct net_device *dev = queue->dev;
  897. cpumask_var_t mask;
  898. int err, i, cpu, pos, map_len, alloc_len, need_set;
  899. unsigned long index;
  900. struct xps_map *map, *new_map;
  901. struct xps_dev_maps *dev_maps, *new_dev_maps;
  902. int nonempty = 0;
  903. int numa_node_id = -2;
  904. if (!capable(CAP_NET_ADMIN))
  905. return -EPERM;
  906. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  907. return -ENOMEM;
  908. index = get_netdev_queue_index(queue);
  909. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  910. if (err) {
  911. free_cpumask_var(mask);
  912. return err;
  913. }
  914. new_dev_maps = kzalloc(max_t(unsigned,
  915. XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
  916. if (!new_dev_maps) {
  917. free_cpumask_var(mask);
  918. return -ENOMEM;
  919. }
  920. mutex_lock(&xps_map_mutex);
  921. dev_maps = xmap_dereference(dev->xps_maps);
  922. for_each_possible_cpu(cpu) {
  923. map = dev_maps ?
  924. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  925. new_map = map;
  926. if (map) {
  927. for (pos = 0; pos < map->len; pos++)
  928. if (map->queues[pos] == index)
  929. break;
  930. map_len = map->len;
  931. alloc_len = map->alloc_len;
  932. } else
  933. pos = map_len = alloc_len = 0;
  934. need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu);
  935. #ifdef CONFIG_NUMA
  936. if (need_set) {
  937. if (numa_node_id == -2)
  938. numa_node_id = cpu_to_node(cpu);
  939. else if (numa_node_id != cpu_to_node(cpu))
  940. numa_node_id = -1;
  941. }
  942. #endif
  943. if (need_set && pos >= map_len) {
  944. /* Need to add queue to this CPU's map */
  945. if (map_len >= alloc_len) {
  946. alloc_len = alloc_len ?
  947. 2 * alloc_len : XPS_MIN_MAP_ALLOC;
  948. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
  949. GFP_KERNEL,
  950. cpu_to_node(cpu));
  951. if (!new_map)
  952. goto error;
  953. new_map->alloc_len = alloc_len;
  954. for (i = 0; i < map_len; i++)
  955. new_map->queues[i] = map->queues[i];
  956. new_map->len = map_len;
  957. }
  958. new_map->queues[new_map->len++] = index;
  959. } else if (!need_set && pos < map_len) {
  960. /* Need to remove queue from this CPU's map */
  961. if (map_len > 1)
  962. new_map->queues[pos] =
  963. new_map->queues[--new_map->len];
  964. else
  965. new_map = NULL;
  966. }
  967. RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
  968. }
  969. /* Cleanup old maps */
  970. for_each_possible_cpu(cpu) {
  971. map = dev_maps ?
  972. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  973. if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
  974. kfree_rcu(map, rcu);
  975. if (new_dev_maps->cpu_map[cpu])
  976. nonempty = 1;
  977. }
  978. if (nonempty)
  979. RCU_INIT_POINTER(dev->xps_maps, new_dev_maps);
  980. else {
  981. kfree(new_dev_maps);
  982. RCU_INIT_POINTER(dev->xps_maps, NULL);
  983. }
  984. if (dev_maps)
  985. kfree_rcu(dev_maps, rcu);
  986. netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id :
  987. NUMA_NO_NODE);
  988. mutex_unlock(&xps_map_mutex);
  989. free_cpumask_var(mask);
  990. return len;
  991. error:
  992. mutex_unlock(&xps_map_mutex);
  993. if (new_dev_maps)
  994. for_each_possible_cpu(i)
  995. kfree(rcu_dereference_protected(
  996. new_dev_maps->cpu_map[i],
  997. 1));
  998. kfree(new_dev_maps);
  999. free_cpumask_var(mask);
  1000. return -ENOMEM;
  1001. }
  1002. static struct netdev_queue_attribute xps_cpus_attribute =
  1003. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  1004. #endif /* CONFIG_XPS */
  1005. static struct attribute *netdev_queue_default_attrs[] = {
  1006. &queue_trans_timeout.attr,
  1007. #ifdef CONFIG_XPS
  1008. &xps_cpus_attribute.attr,
  1009. #endif
  1010. NULL
  1011. };
  1012. static void netdev_queue_release(struct kobject *kobj)
  1013. {
  1014. struct netdev_queue *queue = to_netdev_queue(kobj);
  1015. #ifdef CONFIG_XPS
  1016. xps_queue_release(queue);
  1017. #endif
  1018. memset(kobj, 0, sizeof(*kobj));
  1019. dev_put(queue->dev);
  1020. }
  1021. static struct kobj_type netdev_queue_ktype = {
  1022. .sysfs_ops = &netdev_queue_sysfs_ops,
  1023. .release = netdev_queue_release,
  1024. .default_attrs = netdev_queue_default_attrs,
  1025. };
  1026. static int netdev_queue_add_kobject(struct net_device *net, int index)
  1027. {
  1028. struct netdev_queue *queue = net->_tx + index;
  1029. struct kobject *kobj = &queue->kobj;
  1030. int error = 0;
  1031. kobj->kset = net->queues_kset;
  1032. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1033. "tx-%u", index);
  1034. if (error)
  1035. goto exit;
  1036. #ifdef CONFIG_BQL
  1037. error = sysfs_create_group(kobj, &dql_group);
  1038. if (error)
  1039. goto exit;
  1040. #endif
  1041. kobject_uevent(kobj, KOBJ_ADD);
  1042. dev_hold(queue->dev);
  1043. return 0;
  1044. exit:
  1045. kobject_put(kobj);
  1046. return error;
  1047. }
  1048. #endif /* CONFIG_SYSFS */
  1049. int
  1050. netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  1051. {
  1052. #ifdef CONFIG_SYSFS
  1053. int i;
  1054. int error = 0;
  1055. for (i = old_num; i < new_num; i++) {
  1056. error = netdev_queue_add_kobject(net, i);
  1057. if (error) {
  1058. new_num = old_num;
  1059. break;
  1060. }
  1061. }
  1062. while (--i >= new_num) {
  1063. struct netdev_queue *queue = net->_tx + i;
  1064. #ifdef CONFIG_BQL
  1065. sysfs_remove_group(&queue->kobj, &dql_group);
  1066. #endif
  1067. kobject_put(&queue->kobj);
  1068. }
  1069. return error;
  1070. #else
  1071. return 0;
  1072. #endif /* CONFIG_SYSFS */
  1073. }
  1074. static int register_queue_kobjects(struct net_device *net)
  1075. {
  1076. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1077. #ifdef CONFIG_SYSFS
  1078. net->queues_kset = kset_create_and_add("queues",
  1079. NULL, &net->dev.kobj);
  1080. if (!net->queues_kset)
  1081. return -ENOMEM;
  1082. #endif
  1083. #ifdef CONFIG_RPS
  1084. real_rx = net->real_num_rx_queues;
  1085. #endif
  1086. real_tx = net->real_num_tx_queues;
  1087. error = net_rx_queue_update_kobjects(net, 0, real_rx);
  1088. if (error)
  1089. goto error;
  1090. rxq = real_rx;
  1091. error = netdev_queue_update_kobjects(net, 0, real_tx);
  1092. if (error)
  1093. goto error;
  1094. txq = real_tx;
  1095. return 0;
  1096. error:
  1097. netdev_queue_update_kobjects(net, txq, 0);
  1098. net_rx_queue_update_kobjects(net, rxq, 0);
  1099. return error;
  1100. }
  1101. static void remove_queue_kobjects(struct net_device *net)
  1102. {
  1103. int real_rx = 0, real_tx = 0;
  1104. #ifdef CONFIG_RPS
  1105. real_rx = net->real_num_rx_queues;
  1106. #endif
  1107. real_tx = net->real_num_tx_queues;
  1108. net_rx_queue_update_kobjects(net, real_rx, 0);
  1109. netdev_queue_update_kobjects(net, real_tx, 0);
  1110. #ifdef CONFIG_SYSFS
  1111. kset_unregister(net->queues_kset);
  1112. #endif
  1113. }
  1114. static void *net_grab_current_ns(void)
  1115. {
  1116. struct net *ns = current->nsproxy->net_ns;
  1117. #ifdef CONFIG_NET_NS
  1118. if (ns)
  1119. atomic_inc(&ns->passive);
  1120. #endif
  1121. return ns;
  1122. }
  1123. static const void *net_initial_ns(void)
  1124. {
  1125. return &init_net;
  1126. }
  1127. static const void *net_netlink_ns(struct sock *sk)
  1128. {
  1129. return sock_net(sk);
  1130. }
  1131. struct kobj_ns_type_operations net_ns_type_operations = {
  1132. .type = KOBJ_NS_TYPE_NET,
  1133. .grab_current_ns = net_grab_current_ns,
  1134. .netlink_ns = net_netlink_ns,
  1135. .initial_ns = net_initial_ns,
  1136. .drop_ns = net_drop_ns,
  1137. };
  1138. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1139. #ifdef CONFIG_HOTPLUG
  1140. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1141. {
  1142. struct net_device *dev = to_net_dev(d);
  1143. int retval;
  1144. /* pass interface to uevent. */
  1145. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1146. if (retval)
  1147. goto exit;
  1148. /* pass ifindex to uevent.
  1149. * ifindex is useful as it won't change (interface name may change)
  1150. * and is what RtNetlink uses natively. */
  1151. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1152. exit:
  1153. return retval;
  1154. }
  1155. #endif
  1156. /*
  1157. * netdev_release -- destroy and free a dead device.
  1158. * Called when last reference to device kobject is gone.
  1159. */
  1160. static void netdev_release(struct device *d)
  1161. {
  1162. struct net_device *dev = to_net_dev(d);
  1163. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1164. kfree(dev->ifalias);
  1165. kfree((char *)dev - dev->padded);
  1166. }
  1167. static const void *net_namespace(struct device *d)
  1168. {
  1169. struct net_device *dev;
  1170. dev = container_of(d, struct net_device, dev);
  1171. return dev_net(dev);
  1172. }
  1173. static struct class net_class = {
  1174. .name = "net",
  1175. .dev_release = netdev_release,
  1176. #ifdef CONFIG_SYSFS
  1177. .dev_attrs = net_class_attributes,
  1178. #endif /* CONFIG_SYSFS */
  1179. #ifdef CONFIG_HOTPLUG
  1180. .dev_uevent = netdev_uevent,
  1181. #endif
  1182. .ns_type = &net_ns_type_operations,
  1183. .namespace = net_namespace,
  1184. };
  1185. /* Delete sysfs entries but hold kobject reference until after all
  1186. * netdev references are gone.
  1187. */
  1188. void netdev_unregister_kobject(struct net_device * net)
  1189. {
  1190. struct device *dev = &(net->dev);
  1191. kobject_get(&dev->kobj);
  1192. remove_queue_kobjects(net);
  1193. device_del(dev);
  1194. }
  1195. /* Create sysfs entries for network device. */
  1196. int netdev_register_kobject(struct net_device *net)
  1197. {
  1198. struct device *dev = &(net->dev);
  1199. const struct attribute_group **groups = net->sysfs_groups;
  1200. int error = 0;
  1201. device_initialize(dev);
  1202. dev->class = &net_class;
  1203. dev->platform_data = net;
  1204. dev->groups = groups;
  1205. dev_set_name(dev, "%s", net->name);
  1206. #ifdef CONFIG_SYSFS
  1207. /* Allow for a device specific group */
  1208. if (*groups)
  1209. groups++;
  1210. *groups++ = &netstat_group;
  1211. #ifdef CONFIG_WIRELESS_EXT_SYSFS
  1212. if (net->ieee80211_ptr)
  1213. *groups++ = &wireless_group;
  1214. #ifdef CONFIG_WIRELESS_EXT
  1215. else if (net->wireless_handlers)
  1216. *groups++ = &wireless_group;
  1217. #endif
  1218. #endif
  1219. #endif /* CONFIG_SYSFS */
  1220. error = device_add(dev);
  1221. if (error)
  1222. return error;
  1223. error = register_queue_kobjects(net);
  1224. if (error) {
  1225. device_del(dev);
  1226. return error;
  1227. }
  1228. return error;
  1229. }
  1230. int netdev_class_create_file(struct class_attribute *class_attr)
  1231. {
  1232. return class_create_file(&net_class, class_attr);
  1233. }
  1234. EXPORT_SYMBOL(netdev_class_create_file);
  1235. void netdev_class_remove_file(struct class_attribute *class_attr)
  1236. {
  1237. class_remove_file(&net_class, class_attr);
  1238. }
  1239. EXPORT_SYMBOL(netdev_class_remove_file);
  1240. int netdev_kobject_init(void)
  1241. {
  1242. kobj_ns_type_register(&net_ns_type_operations);
  1243. return class_register(&net_class);
  1244. }