net-sysfs.c 32 KB

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