net-sysfs.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 ssize_t store_rps_map(struct netdev_rx_queue *queue,
  483. struct rx_queue_attribute *attribute,
  484. const char *buf, size_t len)
  485. {
  486. struct rps_map *old_map, *map;
  487. cpumask_var_t mask;
  488. int err, cpu, i;
  489. static DEFINE_SPINLOCK(rps_map_lock);
  490. if (!capable(CAP_NET_ADMIN))
  491. return -EPERM;
  492. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  493. return -ENOMEM;
  494. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  495. if (err) {
  496. free_cpumask_var(mask);
  497. return err;
  498. }
  499. map = kzalloc(max_t(unsigned,
  500. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  501. GFP_KERNEL);
  502. if (!map) {
  503. free_cpumask_var(mask);
  504. return -ENOMEM;
  505. }
  506. i = 0;
  507. for_each_cpu_and(cpu, mask, cpu_online_mask)
  508. map->cpus[i++] = cpu;
  509. if (i)
  510. map->len = i;
  511. else {
  512. kfree(map);
  513. map = NULL;
  514. }
  515. spin_lock(&rps_map_lock);
  516. old_map = rcu_dereference_protected(queue->rps_map,
  517. lockdep_is_held(&rps_map_lock));
  518. rcu_assign_pointer(queue->rps_map, map);
  519. spin_unlock(&rps_map_lock);
  520. if (old_map)
  521. kfree_rcu(old_map, rcu);
  522. free_cpumask_var(mask);
  523. return len;
  524. }
  525. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  526. struct rx_queue_attribute *attr,
  527. char *buf)
  528. {
  529. struct rps_dev_flow_table *flow_table;
  530. unsigned int val = 0;
  531. rcu_read_lock();
  532. flow_table = rcu_dereference(queue->rps_flow_table);
  533. if (flow_table)
  534. val = flow_table->mask + 1;
  535. rcu_read_unlock();
  536. return sprintf(buf, "%u\n", val);
  537. }
  538. static void rps_dev_flow_table_release_work(struct work_struct *work)
  539. {
  540. struct rps_dev_flow_table *table = container_of(work,
  541. struct rps_dev_flow_table, free_work);
  542. vfree(table);
  543. }
  544. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  545. {
  546. struct rps_dev_flow_table *table = container_of(rcu,
  547. struct rps_dev_flow_table, rcu);
  548. INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
  549. schedule_work(&table->free_work);
  550. }
  551. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  552. struct rx_queue_attribute *attr,
  553. const char *buf, size_t len)
  554. {
  555. unsigned int count;
  556. char *endp;
  557. struct rps_dev_flow_table *table, *old_table;
  558. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  559. if (!capable(CAP_NET_ADMIN))
  560. return -EPERM;
  561. count = simple_strtoul(buf, &endp, 0);
  562. if (endp == buf)
  563. return -EINVAL;
  564. if (count) {
  565. int i;
  566. if (count > 1<<30) {
  567. /* Enforce a limit to prevent overflow */
  568. return -EINVAL;
  569. }
  570. count = roundup_pow_of_two(count);
  571. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
  572. if (!table)
  573. return -ENOMEM;
  574. table->mask = count - 1;
  575. for (i = 0; i < count; i++)
  576. table->flows[i].cpu = RPS_NO_CPU;
  577. } else
  578. table = NULL;
  579. spin_lock(&rps_dev_flow_lock);
  580. old_table = rcu_dereference_protected(queue->rps_flow_table,
  581. lockdep_is_held(&rps_dev_flow_lock));
  582. rcu_assign_pointer(queue->rps_flow_table, table);
  583. spin_unlock(&rps_dev_flow_lock);
  584. if (old_table)
  585. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  586. return len;
  587. }
  588. static struct rx_queue_attribute rps_cpus_attribute =
  589. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  590. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  591. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  592. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  593. static struct attribute *rx_queue_default_attrs[] = {
  594. &rps_cpus_attribute.attr,
  595. &rps_dev_flow_table_cnt_attribute.attr,
  596. NULL
  597. };
  598. static void rx_queue_release(struct kobject *kobj)
  599. {
  600. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  601. struct rps_map *map;
  602. struct rps_dev_flow_table *flow_table;
  603. map = rcu_dereference_raw(queue->rps_map);
  604. if (map) {
  605. RCU_INIT_POINTER(queue->rps_map, NULL);
  606. kfree_rcu(map, rcu);
  607. }
  608. flow_table = rcu_dereference_raw(queue->rps_flow_table);
  609. if (flow_table) {
  610. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  611. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  612. }
  613. memset(kobj, 0, sizeof(*kobj));
  614. dev_put(queue->dev);
  615. }
  616. static struct kobj_type rx_queue_ktype = {
  617. .sysfs_ops = &rx_queue_sysfs_ops,
  618. .release = rx_queue_release,
  619. .default_attrs = rx_queue_default_attrs,
  620. };
  621. static int rx_queue_add_kobject(struct net_device *net, int index)
  622. {
  623. struct netdev_rx_queue *queue = net->_rx + index;
  624. struct kobject *kobj = &queue->kobj;
  625. int error = 0;
  626. kobj->kset = net->queues_kset;
  627. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  628. "rx-%u", index);
  629. if (error) {
  630. kobject_put(kobj);
  631. return error;
  632. }
  633. kobject_uevent(kobj, KOBJ_ADD);
  634. dev_hold(queue->dev);
  635. return error;
  636. }
  637. #endif /* CONFIG_RPS */
  638. int
  639. net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  640. {
  641. #ifdef CONFIG_RPS
  642. int i;
  643. int error = 0;
  644. for (i = old_num; i < new_num; i++) {
  645. error = rx_queue_add_kobject(net, i);
  646. if (error) {
  647. new_num = old_num;
  648. break;
  649. }
  650. }
  651. while (--i >= new_num)
  652. kobject_put(&net->_rx[i].kobj);
  653. return error;
  654. #else
  655. return 0;
  656. #endif
  657. }
  658. #ifdef CONFIG_XPS
  659. /*
  660. * netdev_queue sysfs structures and functions.
  661. */
  662. struct netdev_queue_attribute {
  663. struct attribute attr;
  664. ssize_t (*show)(struct netdev_queue *queue,
  665. struct netdev_queue_attribute *attr, char *buf);
  666. ssize_t (*store)(struct netdev_queue *queue,
  667. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  668. };
  669. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  670. struct netdev_queue_attribute, attr)
  671. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  672. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  673. struct attribute *attr, char *buf)
  674. {
  675. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  676. struct netdev_queue *queue = to_netdev_queue(kobj);
  677. if (!attribute->show)
  678. return -EIO;
  679. return attribute->show(queue, attribute, buf);
  680. }
  681. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  682. struct attribute *attr,
  683. const char *buf, size_t count)
  684. {
  685. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  686. struct netdev_queue *queue = to_netdev_queue(kobj);
  687. if (!attribute->store)
  688. return -EIO;
  689. return attribute->store(queue, attribute, buf, count);
  690. }
  691. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  692. .show = netdev_queue_attr_show,
  693. .store = netdev_queue_attr_store,
  694. };
  695. static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  696. {
  697. struct net_device *dev = queue->dev;
  698. int i;
  699. for (i = 0; i < dev->num_tx_queues; i++)
  700. if (queue == &dev->_tx[i])
  701. break;
  702. BUG_ON(i >= dev->num_tx_queues);
  703. return i;
  704. }
  705. static ssize_t show_xps_map(struct netdev_queue *queue,
  706. struct netdev_queue_attribute *attribute, char *buf)
  707. {
  708. struct net_device *dev = queue->dev;
  709. struct xps_dev_maps *dev_maps;
  710. cpumask_var_t mask;
  711. unsigned long index;
  712. size_t len = 0;
  713. int i;
  714. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  715. return -ENOMEM;
  716. index = get_netdev_queue_index(queue);
  717. rcu_read_lock();
  718. dev_maps = rcu_dereference(dev->xps_maps);
  719. if (dev_maps) {
  720. for_each_possible_cpu(i) {
  721. struct xps_map *map =
  722. rcu_dereference(dev_maps->cpu_map[i]);
  723. if (map) {
  724. int j;
  725. for (j = 0; j < map->len; j++) {
  726. if (map->queues[j] == index) {
  727. cpumask_set_cpu(i, mask);
  728. break;
  729. }
  730. }
  731. }
  732. }
  733. }
  734. rcu_read_unlock();
  735. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  736. if (PAGE_SIZE - len < 3) {
  737. free_cpumask_var(mask);
  738. return -EINVAL;
  739. }
  740. free_cpumask_var(mask);
  741. len += sprintf(buf + len, "\n");
  742. return len;
  743. }
  744. static void xps_dev_maps_release(struct rcu_head *rcu)
  745. {
  746. struct xps_dev_maps *dev_maps =
  747. container_of(rcu, struct xps_dev_maps, rcu);
  748. kfree(dev_maps);
  749. }
  750. static DEFINE_MUTEX(xps_map_mutex);
  751. #define xmap_dereference(P) \
  752. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  753. static ssize_t store_xps_map(struct netdev_queue *queue,
  754. struct netdev_queue_attribute *attribute,
  755. const char *buf, size_t len)
  756. {
  757. struct net_device *dev = queue->dev;
  758. cpumask_var_t mask;
  759. int err, i, cpu, pos, map_len, alloc_len, need_set;
  760. unsigned long index;
  761. struct xps_map *map, *new_map;
  762. struct xps_dev_maps *dev_maps, *new_dev_maps;
  763. int nonempty = 0;
  764. int numa_node = -2;
  765. if (!capable(CAP_NET_ADMIN))
  766. return -EPERM;
  767. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  768. return -ENOMEM;
  769. index = get_netdev_queue_index(queue);
  770. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  771. if (err) {
  772. free_cpumask_var(mask);
  773. return err;
  774. }
  775. new_dev_maps = kzalloc(max_t(unsigned,
  776. XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
  777. if (!new_dev_maps) {
  778. free_cpumask_var(mask);
  779. return -ENOMEM;
  780. }
  781. mutex_lock(&xps_map_mutex);
  782. dev_maps = xmap_dereference(dev->xps_maps);
  783. for_each_possible_cpu(cpu) {
  784. map = dev_maps ?
  785. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  786. new_map = map;
  787. if (map) {
  788. for (pos = 0; pos < map->len; pos++)
  789. if (map->queues[pos] == index)
  790. break;
  791. map_len = map->len;
  792. alloc_len = map->alloc_len;
  793. } else
  794. pos = map_len = alloc_len = 0;
  795. need_set = cpu_isset(cpu, *mask) && cpu_online(cpu);
  796. #ifdef CONFIG_NUMA
  797. if (need_set) {
  798. if (numa_node == -2)
  799. numa_node = cpu_to_node(cpu);
  800. else if (numa_node != cpu_to_node(cpu))
  801. numa_node = -1;
  802. }
  803. #endif
  804. if (need_set && pos >= map_len) {
  805. /* Need to add queue to this CPU's map */
  806. if (map_len >= alloc_len) {
  807. alloc_len = alloc_len ?
  808. 2 * alloc_len : XPS_MIN_MAP_ALLOC;
  809. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
  810. GFP_KERNEL,
  811. cpu_to_node(cpu));
  812. if (!new_map)
  813. goto error;
  814. new_map->alloc_len = alloc_len;
  815. for (i = 0; i < map_len; i++)
  816. new_map->queues[i] = map->queues[i];
  817. new_map->len = map_len;
  818. }
  819. new_map->queues[new_map->len++] = index;
  820. } else if (!need_set && pos < map_len) {
  821. /* Need to remove queue from this CPU's map */
  822. if (map_len > 1)
  823. new_map->queues[pos] =
  824. new_map->queues[--new_map->len];
  825. else
  826. new_map = NULL;
  827. }
  828. RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
  829. }
  830. /* Cleanup old maps */
  831. for_each_possible_cpu(cpu) {
  832. map = dev_maps ?
  833. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  834. if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
  835. kfree_rcu(map, rcu);
  836. if (new_dev_maps->cpu_map[cpu])
  837. nonempty = 1;
  838. }
  839. if (nonempty)
  840. rcu_assign_pointer(dev->xps_maps, new_dev_maps);
  841. else {
  842. kfree(new_dev_maps);
  843. rcu_assign_pointer(dev->xps_maps, NULL);
  844. }
  845. if (dev_maps)
  846. call_rcu(&dev_maps->rcu, xps_dev_maps_release);
  847. netdev_queue_numa_node_write(queue, (numa_node >= 0) ? numa_node :
  848. NUMA_NO_NODE);
  849. mutex_unlock(&xps_map_mutex);
  850. free_cpumask_var(mask);
  851. return len;
  852. error:
  853. mutex_unlock(&xps_map_mutex);
  854. if (new_dev_maps)
  855. for_each_possible_cpu(i)
  856. kfree(rcu_dereference_protected(
  857. new_dev_maps->cpu_map[i],
  858. 1));
  859. kfree(new_dev_maps);
  860. free_cpumask_var(mask);
  861. return -ENOMEM;
  862. }
  863. static struct netdev_queue_attribute xps_cpus_attribute =
  864. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  865. static struct attribute *netdev_queue_default_attrs[] = {
  866. &xps_cpus_attribute.attr,
  867. NULL
  868. };
  869. static void netdev_queue_release(struct kobject *kobj)
  870. {
  871. struct netdev_queue *queue = to_netdev_queue(kobj);
  872. struct net_device *dev = queue->dev;
  873. struct xps_dev_maps *dev_maps;
  874. struct xps_map *map;
  875. unsigned long index;
  876. int i, pos, nonempty = 0;
  877. index = get_netdev_queue_index(queue);
  878. mutex_lock(&xps_map_mutex);
  879. dev_maps = xmap_dereference(dev->xps_maps);
  880. if (dev_maps) {
  881. for_each_possible_cpu(i) {
  882. map = xmap_dereference(dev_maps->cpu_map[i]);
  883. if (!map)
  884. continue;
  885. for (pos = 0; pos < map->len; pos++)
  886. if (map->queues[pos] == index)
  887. break;
  888. if (pos < map->len) {
  889. if (map->len > 1)
  890. map->queues[pos] =
  891. map->queues[--map->len];
  892. else {
  893. RCU_INIT_POINTER(dev_maps->cpu_map[i],
  894. NULL);
  895. kfree_rcu(map, rcu);
  896. map = NULL;
  897. }
  898. }
  899. if (map)
  900. nonempty = 1;
  901. }
  902. if (!nonempty) {
  903. RCU_INIT_POINTER(dev->xps_maps, NULL);
  904. call_rcu(&dev_maps->rcu, xps_dev_maps_release);
  905. }
  906. }
  907. mutex_unlock(&xps_map_mutex);
  908. memset(kobj, 0, sizeof(*kobj));
  909. dev_put(queue->dev);
  910. }
  911. static struct kobj_type netdev_queue_ktype = {
  912. .sysfs_ops = &netdev_queue_sysfs_ops,
  913. .release = netdev_queue_release,
  914. .default_attrs = netdev_queue_default_attrs,
  915. };
  916. static int netdev_queue_add_kobject(struct net_device *net, int index)
  917. {
  918. struct netdev_queue *queue = net->_tx + index;
  919. struct kobject *kobj = &queue->kobj;
  920. int error = 0;
  921. kobj->kset = net->queues_kset;
  922. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  923. "tx-%u", index);
  924. if (error) {
  925. kobject_put(kobj);
  926. return error;
  927. }
  928. kobject_uevent(kobj, KOBJ_ADD);
  929. dev_hold(queue->dev);
  930. return error;
  931. }
  932. #endif /* CONFIG_XPS */
  933. int
  934. netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  935. {
  936. #ifdef CONFIG_XPS
  937. int i;
  938. int error = 0;
  939. for (i = old_num; i < new_num; i++) {
  940. error = netdev_queue_add_kobject(net, i);
  941. if (error) {
  942. new_num = old_num;
  943. break;
  944. }
  945. }
  946. while (--i >= new_num)
  947. kobject_put(&net->_tx[i].kobj);
  948. return error;
  949. #else
  950. return 0;
  951. #endif
  952. }
  953. static int register_queue_kobjects(struct net_device *net)
  954. {
  955. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  956. #if defined(CONFIG_RPS) || defined(CONFIG_XPS)
  957. net->queues_kset = kset_create_and_add("queues",
  958. NULL, &net->dev.kobj);
  959. if (!net->queues_kset)
  960. return -ENOMEM;
  961. #endif
  962. #ifdef CONFIG_RPS
  963. real_rx = net->real_num_rx_queues;
  964. #endif
  965. real_tx = net->real_num_tx_queues;
  966. error = net_rx_queue_update_kobjects(net, 0, real_rx);
  967. if (error)
  968. goto error;
  969. rxq = real_rx;
  970. error = netdev_queue_update_kobjects(net, 0, real_tx);
  971. if (error)
  972. goto error;
  973. txq = real_tx;
  974. return 0;
  975. error:
  976. netdev_queue_update_kobjects(net, txq, 0);
  977. net_rx_queue_update_kobjects(net, rxq, 0);
  978. return error;
  979. }
  980. static void remove_queue_kobjects(struct net_device *net)
  981. {
  982. int real_rx = 0, real_tx = 0;
  983. #ifdef CONFIG_RPS
  984. real_rx = net->real_num_rx_queues;
  985. #endif
  986. real_tx = net->real_num_tx_queues;
  987. net_rx_queue_update_kobjects(net, real_rx, 0);
  988. netdev_queue_update_kobjects(net, real_tx, 0);
  989. #if defined(CONFIG_RPS) || defined(CONFIG_XPS)
  990. kset_unregister(net->queues_kset);
  991. #endif
  992. }
  993. static const void *net_current_ns(void)
  994. {
  995. return current->nsproxy->net_ns;
  996. }
  997. static const void *net_initial_ns(void)
  998. {
  999. return &init_net;
  1000. }
  1001. static const void *net_netlink_ns(struct sock *sk)
  1002. {
  1003. return sock_net(sk);
  1004. }
  1005. struct kobj_ns_type_operations net_ns_type_operations = {
  1006. .type = KOBJ_NS_TYPE_NET,
  1007. .current_ns = net_current_ns,
  1008. .netlink_ns = net_netlink_ns,
  1009. .initial_ns = net_initial_ns,
  1010. };
  1011. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1012. static void net_kobj_ns_exit(struct net *net)
  1013. {
  1014. kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
  1015. }
  1016. static struct pernet_operations kobj_net_ops = {
  1017. .exit = net_kobj_ns_exit,
  1018. };
  1019. #ifdef CONFIG_HOTPLUG
  1020. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1021. {
  1022. struct net_device *dev = to_net_dev(d);
  1023. int retval;
  1024. /* pass interface to uevent. */
  1025. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1026. if (retval)
  1027. goto exit;
  1028. /* pass ifindex to uevent.
  1029. * ifindex is useful as it won't change (interface name may change)
  1030. * and is what RtNetlink uses natively. */
  1031. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1032. exit:
  1033. return retval;
  1034. }
  1035. #endif
  1036. /*
  1037. * netdev_release -- destroy and free a dead device.
  1038. * Called when last reference to device kobject is gone.
  1039. */
  1040. static void netdev_release(struct device *d)
  1041. {
  1042. struct net_device *dev = to_net_dev(d);
  1043. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1044. kfree(dev->ifalias);
  1045. kfree((char *)dev - dev->padded);
  1046. }
  1047. static const void *net_namespace(struct device *d)
  1048. {
  1049. struct net_device *dev;
  1050. dev = container_of(d, struct net_device, dev);
  1051. return dev_net(dev);
  1052. }
  1053. static struct class net_class = {
  1054. .name = "net",
  1055. .dev_release = netdev_release,
  1056. #ifdef CONFIG_SYSFS
  1057. .dev_attrs = net_class_attributes,
  1058. #endif /* CONFIG_SYSFS */
  1059. #ifdef CONFIG_HOTPLUG
  1060. .dev_uevent = netdev_uevent,
  1061. #endif
  1062. .ns_type = &net_ns_type_operations,
  1063. .namespace = net_namespace,
  1064. };
  1065. /* Delete sysfs entries but hold kobject reference until after all
  1066. * netdev references are gone.
  1067. */
  1068. void netdev_unregister_kobject(struct net_device * net)
  1069. {
  1070. struct device *dev = &(net->dev);
  1071. kobject_get(&dev->kobj);
  1072. remove_queue_kobjects(net);
  1073. device_del(dev);
  1074. }
  1075. /* Create sysfs entries for network device. */
  1076. int netdev_register_kobject(struct net_device *net)
  1077. {
  1078. struct device *dev = &(net->dev);
  1079. const struct attribute_group **groups = net->sysfs_groups;
  1080. int error = 0;
  1081. device_initialize(dev);
  1082. dev->class = &net_class;
  1083. dev->platform_data = net;
  1084. dev->groups = groups;
  1085. dev_set_name(dev, "%s", net->name);
  1086. #ifdef CONFIG_SYSFS
  1087. /* Allow for a device specific group */
  1088. if (*groups)
  1089. groups++;
  1090. *groups++ = &netstat_group;
  1091. #ifdef CONFIG_WIRELESS_EXT_SYSFS
  1092. if (net->ieee80211_ptr)
  1093. *groups++ = &wireless_group;
  1094. #ifdef CONFIG_WIRELESS_EXT
  1095. else if (net->wireless_handlers)
  1096. *groups++ = &wireless_group;
  1097. #endif
  1098. #endif
  1099. #endif /* CONFIG_SYSFS */
  1100. error = device_add(dev);
  1101. if (error)
  1102. return error;
  1103. error = register_queue_kobjects(net);
  1104. if (error) {
  1105. device_del(dev);
  1106. return error;
  1107. }
  1108. return error;
  1109. }
  1110. int netdev_class_create_file(struct class_attribute *class_attr)
  1111. {
  1112. return class_create_file(&net_class, class_attr);
  1113. }
  1114. EXPORT_SYMBOL(netdev_class_create_file);
  1115. void netdev_class_remove_file(struct class_attribute *class_attr)
  1116. {
  1117. class_remove_file(&net_class, class_attr);
  1118. }
  1119. EXPORT_SYMBOL(netdev_class_remove_file);
  1120. int netdev_kobject_init(void)
  1121. {
  1122. kobj_ns_type_register(&net_ns_type_operations);
  1123. register_pernet_subsys(&kobj_net_ops);
  1124. return class_register(&net_class);
  1125. }