net-sysfs.c 32 KB

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