net-sysfs.c 32 KB

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