net-sysfs.c 34 KB

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