net-sysfs.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 <linux/jiffies.h>
  24. #include <net/wext.h>
  25. #include "net-sysfs.h"
  26. #ifdef CONFIG_SYSFS
  27. static const char fmt_hex[] = "%#x\n";
  28. static const char fmt_long_hex[] = "%#lx\n";
  29. static const char fmt_dec[] = "%d\n";
  30. static const char fmt_udec[] = "%u\n";
  31. static const char fmt_ulong[] = "%lu\n";
  32. static const char fmt_u64[] = "%llu\n";
  33. static inline int dev_isalive(const struct net_device *dev)
  34. {
  35. return dev->reg_state <= NETREG_REGISTERED;
  36. }
  37. /* use same locking rules as GIF* ioctl's */
  38. static ssize_t netdev_show(const struct device *dev,
  39. struct device_attribute *attr, char *buf,
  40. ssize_t (*format)(const struct net_device *, char *))
  41. {
  42. struct net_device *net = to_net_dev(dev);
  43. ssize_t ret = -EINVAL;
  44. read_lock(&dev_base_lock);
  45. if (dev_isalive(net))
  46. ret = (*format)(net, buf);
  47. read_unlock(&dev_base_lock);
  48. return ret;
  49. }
  50. /* generate a show function for simple field */
  51. #define NETDEVICE_SHOW(field, format_string) \
  52. static ssize_t format_##field(const struct net_device *net, char *buf) \
  53. { \
  54. return sprintf(buf, format_string, net->field); \
  55. } \
  56. static ssize_t show_##field(struct device *dev, \
  57. struct device_attribute *attr, char *buf) \
  58. { \
  59. return netdev_show(dev, attr, buf, format_##field); \
  60. }
  61. /* use same locking and permission rules as SIF* ioctl's */
  62. static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  63. const char *buf, size_t len,
  64. int (*set)(struct net_device *, unsigned long))
  65. {
  66. struct net_device *net = to_net_dev(dev);
  67. unsigned long new;
  68. int ret = -EINVAL;
  69. if (!capable(CAP_NET_ADMIN))
  70. return -EPERM;
  71. ret = kstrtoul(buf, 0, &new);
  72. if (ret)
  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 int) 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. #endif /* CONFIG_SYSFS */
  359. #ifdef CONFIG_RPS
  360. /*
  361. * RX queue sysfs structures and functions.
  362. */
  363. struct rx_queue_attribute {
  364. struct attribute attr;
  365. ssize_t (*show)(struct netdev_rx_queue *queue,
  366. struct rx_queue_attribute *attr, char *buf);
  367. ssize_t (*store)(struct netdev_rx_queue *queue,
  368. struct rx_queue_attribute *attr, const char *buf, size_t len);
  369. };
  370. #define to_rx_queue_attr(_attr) container_of(_attr, \
  371. struct rx_queue_attribute, attr)
  372. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  373. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  374. char *buf)
  375. {
  376. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  377. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  378. if (!attribute->show)
  379. return -EIO;
  380. return attribute->show(queue, attribute, buf);
  381. }
  382. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  383. const char *buf, size_t count)
  384. {
  385. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  386. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  387. if (!attribute->store)
  388. return -EIO;
  389. return attribute->store(queue, attribute, buf, count);
  390. }
  391. static const struct sysfs_ops rx_queue_sysfs_ops = {
  392. .show = rx_queue_attr_show,
  393. .store = rx_queue_attr_store,
  394. };
  395. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  396. struct rx_queue_attribute *attribute, char *buf)
  397. {
  398. struct rps_map *map;
  399. cpumask_var_t mask;
  400. size_t len = 0;
  401. int i;
  402. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  403. return -ENOMEM;
  404. rcu_read_lock();
  405. map = rcu_dereference(queue->rps_map);
  406. if (map)
  407. for (i = 0; i < map->len; i++)
  408. cpumask_set_cpu(map->cpus[i], mask);
  409. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  410. if (PAGE_SIZE - len < 3) {
  411. rcu_read_unlock();
  412. free_cpumask_var(mask);
  413. return -EINVAL;
  414. }
  415. rcu_read_unlock();
  416. free_cpumask_var(mask);
  417. len += sprintf(buf + len, "\n");
  418. return len;
  419. }
  420. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  421. struct rx_queue_attribute *attribute,
  422. const char *buf, size_t len)
  423. {
  424. struct rps_map *old_map, *map;
  425. cpumask_var_t mask;
  426. int err, cpu, i;
  427. static DEFINE_SPINLOCK(rps_map_lock);
  428. if (!capable(CAP_NET_ADMIN))
  429. return -EPERM;
  430. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  431. return -ENOMEM;
  432. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  433. if (err) {
  434. free_cpumask_var(mask);
  435. return err;
  436. }
  437. map = kzalloc(max_t(unsigned int,
  438. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  439. GFP_KERNEL);
  440. if (!map) {
  441. free_cpumask_var(mask);
  442. return -ENOMEM;
  443. }
  444. i = 0;
  445. for_each_cpu_and(cpu, mask, cpu_online_mask)
  446. map->cpus[i++] = cpu;
  447. if (i)
  448. map->len = i;
  449. else {
  450. kfree(map);
  451. map = NULL;
  452. }
  453. spin_lock(&rps_map_lock);
  454. old_map = rcu_dereference_protected(queue->rps_map,
  455. lockdep_is_held(&rps_map_lock));
  456. rcu_assign_pointer(queue->rps_map, map);
  457. spin_unlock(&rps_map_lock);
  458. if (map)
  459. static_key_slow_inc(&rps_needed);
  460. if (old_map) {
  461. kfree_rcu(old_map, rcu);
  462. static_key_slow_dec(&rps_needed);
  463. }
  464. free_cpumask_var(mask);
  465. return len;
  466. }
  467. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  468. struct rx_queue_attribute *attr,
  469. char *buf)
  470. {
  471. struct rps_dev_flow_table *flow_table;
  472. unsigned long val = 0;
  473. rcu_read_lock();
  474. flow_table = rcu_dereference(queue->rps_flow_table);
  475. if (flow_table)
  476. val = (unsigned long)flow_table->mask + 1;
  477. rcu_read_unlock();
  478. return sprintf(buf, "%lu\n", val);
  479. }
  480. static void rps_dev_flow_table_release_work(struct work_struct *work)
  481. {
  482. struct rps_dev_flow_table *table = container_of(work,
  483. struct rps_dev_flow_table, free_work);
  484. vfree(table);
  485. }
  486. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  487. {
  488. struct rps_dev_flow_table *table = container_of(rcu,
  489. struct rps_dev_flow_table, rcu);
  490. INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
  491. schedule_work(&table->free_work);
  492. }
  493. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  494. struct rx_queue_attribute *attr,
  495. const char *buf, size_t len)
  496. {
  497. unsigned long mask, count;
  498. struct rps_dev_flow_table *table, *old_table;
  499. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  500. int rc;
  501. if (!capable(CAP_NET_ADMIN))
  502. return -EPERM;
  503. rc = kstrtoul(buf, 0, &count);
  504. if (rc < 0)
  505. return rc;
  506. if (count) {
  507. mask = count - 1;
  508. /* mask = roundup_pow_of_two(count) - 1;
  509. * without overflows...
  510. */
  511. while ((mask | (mask >> 1)) != mask)
  512. mask |= (mask >> 1);
  513. /* On 64 bit arches, must check mask fits in table->mask (u32),
  514. * and on 32bit arches, must check RPS_DEV_FLOW_TABLE_SIZE(mask + 1)
  515. * doesnt overflow.
  516. */
  517. #if BITS_PER_LONG > 32
  518. if (mask > (unsigned long)(u32)mask)
  519. return -EINVAL;
  520. #else
  521. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  522. / sizeof(struct rps_dev_flow)) {
  523. /* Enforce a limit to prevent overflow */
  524. return -EINVAL;
  525. }
  526. #endif
  527. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  528. if (!table)
  529. return -ENOMEM;
  530. table->mask = mask;
  531. for (count = 0; count <= mask; count++)
  532. table->flows[count].cpu = RPS_NO_CPU;
  533. } else
  534. table = NULL;
  535. spin_lock(&rps_dev_flow_lock);
  536. old_table = rcu_dereference_protected(queue->rps_flow_table,
  537. lockdep_is_held(&rps_dev_flow_lock));
  538. rcu_assign_pointer(queue->rps_flow_table, table);
  539. spin_unlock(&rps_dev_flow_lock);
  540. if (old_table)
  541. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  542. return len;
  543. }
  544. static struct rx_queue_attribute rps_cpus_attribute =
  545. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  546. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  547. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  548. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  549. static struct attribute *rx_queue_default_attrs[] = {
  550. &rps_cpus_attribute.attr,
  551. &rps_dev_flow_table_cnt_attribute.attr,
  552. NULL
  553. };
  554. static void rx_queue_release(struct kobject *kobj)
  555. {
  556. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  557. struct rps_map *map;
  558. struct rps_dev_flow_table *flow_table;
  559. map = rcu_dereference_protected(queue->rps_map, 1);
  560. if (map) {
  561. RCU_INIT_POINTER(queue->rps_map, NULL);
  562. kfree_rcu(map, rcu);
  563. }
  564. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  565. if (flow_table) {
  566. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  567. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  568. }
  569. memset(kobj, 0, sizeof(*kobj));
  570. dev_put(queue->dev);
  571. }
  572. static struct kobj_type rx_queue_ktype = {
  573. .sysfs_ops = &rx_queue_sysfs_ops,
  574. .release = rx_queue_release,
  575. .default_attrs = rx_queue_default_attrs,
  576. };
  577. static int rx_queue_add_kobject(struct net_device *net, int index)
  578. {
  579. struct netdev_rx_queue *queue = net->_rx + index;
  580. struct kobject *kobj = &queue->kobj;
  581. int error = 0;
  582. kobj->kset = net->queues_kset;
  583. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  584. "rx-%u", index);
  585. if (error) {
  586. kobject_put(kobj);
  587. return error;
  588. }
  589. kobject_uevent(kobj, KOBJ_ADD);
  590. dev_hold(queue->dev);
  591. return error;
  592. }
  593. #endif /* CONFIG_RPS */
  594. int
  595. net_rx_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  596. {
  597. #ifdef CONFIG_RPS
  598. int i;
  599. int error = 0;
  600. for (i = old_num; i < new_num; i++) {
  601. error = rx_queue_add_kobject(net, i);
  602. if (error) {
  603. new_num = old_num;
  604. break;
  605. }
  606. }
  607. while (--i >= new_num)
  608. kobject_put(&net->_rx[i].kobj);
  609. return error;
  610. #else
  611. return 0;
  612. #endif
  613. }
  614. #ifdef CONFIG_SYSFS
  615. /*
  616. * netdev_queue sysfs structures and functions.
  617. */
  618. struct netdev_queue_attribute {
  619. struct attribute attr;
  620. ssize_t (*show)(struct netdev_queue *queue,
  621. struct netdev_queue_attribute *attr, char *buf);
  622. ssize_t (*store)(struct netdev_queue *queue,
  623. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  624. };
  625. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  626. struct netdev_queue_attribute, attr)
  627. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  628. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  629. struct attribute *attr, char *buf)
  630. {
  631. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  632. struct netdev_queue *queue = to_netdev_queue(kobj);
  633. if (!attribute->show)
  634. return -EIO;
  635. return attribute->show(queue, attribute, buf);
  636. }
  637. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  638. struct attribute *attr,
  639. const char *buf, size_t count)
  640. {
  641. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  642. struct netdev_queue *queue = to_netdev_queue(kobj);
  643. if (!attribute->store)
  644. return -EIO;
  645. return attribute->store(queue, attribute, buf, count);
  646. }
  647. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  648. .show = netdev_queue_attr_show,
  649. .store = netdev_queue_attr_store,
  650. };
  651. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  652. struct netdev_queue_attribute *attribute,
  653. char *buf)
  654. {
  655. unsigned long trans_timeout;
  656. spin_lock_irq(&queue->_xmit_lock);
  657. trans_timeout = queue->trans_timeout;
  658. spin_unlock_irq(&queue->_xmit_lock);
  659. return sprintf(buf, "%lu", trans_timeout);
  660. }
  661. static struct netdev_queue_attribute queue_trans_timeout =
  662. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  663. #ifdef CONFIG_BQL
  664. /*
  665. * Byte queue limits sysfs structures and functions.
  666. */
  667. static ssize_t bql_show(char *buf, unsigned int value)
  668. {
  669. return sprintf(buf, "%u\n", value);
  670. }
  671. static ssize_t bql_set(const char *buf, const size_t count,
  672. unsigned int *pvalue)
  673. {
  674. unsigned int value;
  675. int err;
  676. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  677. value = DQL_MAX_LIMIT;
  678. else {
  679. err = kstrtouint(buf, 10, &value);
  680. if (err < 0)
  681. return err;
  682. if (value > DQL_MAX_LIMIT)
  683. return -EINVAL;
  684. }
  685. *pvalue = value;
  686. return count;
  687. }
  688. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  689. struct netdev_queue_attribute *attr,
  690. char *buf)
  691. {
  692. struct dql *dql = &queue->dql;
  693. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  694. }
  695. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  696. struct netdev_queue_attribute *attribute,
  697. const char *buf, size_t len)
  698. {
  699. struct dql *dql = &queue->dql;
  700. unsigned int value;
  701. int err;
  702. err = kstrtouint(buf, 10, &value);
  703. if (err < 0)
  704. return err;
  705. dql->slack_hold_time = msecs_to_jiffies(value);
  706. return len;
  707. }
  708. static struct netdev_queue_attribute bql_hold_time_attribute =
  709. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  710. bql_set_hold_time);
  711. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  712. struct netdev_queue_attribute *attr,
  713. char *buf)
  714. {
  715. struct dql *dql = &queue->dql;
  716. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  717. }
  718. static struct netdev_queue_attribute bql_inflight_attribute =
  719. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  720. #define BQL_ATTR(NAME, FIELD) \
  721. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  722. struct netdev_queue_attribute *attr, \
  723. char *buf) \
  724. { \
  725. return bql_show(buf, queue->dql.FIELD); \
  726. } \
  727. \
  728. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  729. struct netdev_queue_attribute *attr, \
  730. const char *buf, size_t len) \
  731. { \
  732. return bql_set(buf, len, &queue->dql.FIELD); \
  733. } \
  734. \
  735. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  736. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  737. bql_set_ ## NAME);
  738. BQL_ATTR(limit, limit)
  739. BQL_ATTR(limit_max, max_limit)
  740. BQL_ATTR(limit_min, min_limit)
  741. static struct attribute *dql_attrs[] = {
  742. &bql_limit_attribute.attr,
  743. &bql_limit_max_attribute.attr,
  744. &bql_limit_min_attribute.attr,
  745. &bql_hold_time_attribute.attr,
  746. &bql_inflight_attribute.attr,
  747. NULL
  748. };
  749. static struct attribute_group dql_group = {
  750. .name = "byte_queue_limits",
  751. .attrs = dql_attrs,
  752. };
  753. #endif /* CONFIG_BQL */
  754. #ifdef CONFIG_XPS
  755. static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  756. {
  757. struct net_device *dev = queue->dev;
  758. int i;
  759. for (i = 0; i < dev->num_tx_queues; i++)
  760. if (queue == &dev->_tx[i])
  761. break;
  762. BUG_ON(i >= dev->num_tx_queues);
  763. return i;
  764. }
  765. static ssize_t show_xps_map(struct netdev_queue *queue,
  766. struct netdev_queue_attribute *attribute, char *buf)
  767. {
  768. struct net_device *dev = queue->dev;
  769. struct xps_dev_maps *dev_maps;
  770. cpumask_var_t mask;
  771. unsigned long index;
  772. size_t len = 0;
  773. int i;
  774. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  775. return -ENOMEM;
  776. index = get_netdev_queue_index(queue);
  777. rcu_read_lock();
  778. dev_maps = rcu_dereference(dev->xps_maps);
  779. if (dev_maps) {
  780. for_each_possible_cpu(i) {
  781. struct xps_map *map =
  782. rcu_dereference(dev_maps->cpu_map[i]);
  783. if (map) {
  784. int j;
  785. for (j = 0; j < map->len; j++) {
  786. if (map->queues[j] == index) {
  787. cpumask_set_cpu(i, mask);
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. rcu_read_unlock();
  795. len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
  796. if (PAGE_SIZE - len < 3) {
  797. free_cpumask_var(mask);
  798. return -EINVAL;
  799. }
  800. free_cpumask_var(mask);
  801. len += sprintf(buf + len, "\n");
  802. return len;
  803. }
  804. static DEFINE_MUTEX(xps_map_mutex);
  805. #define xmap_dereference(P) \
  806. rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
  807. static void xps_queue_release(struct netdev_queue *queue)
  808. {
  809. struct net_device *dev = queue->dev;
  810. struct xps_dev_maps *dev_maps;
  811. struct xps_map *map;
  812. unsigned long index;
  813. int i, pos, nonempty = 0;
  814. index = get_netdev_queue_index(queue);
  815. mutex_lock(&xps_map_mutex);
  816. dev_maps = xmap_dereference(dev->xps_maps);
  817. if (dev_maps) {
  818. for_each_possible_cpu(i) {
  819. map = xmap_dereference(dev_maps->cpu_map[i]);
  820. if (!map)
  821. continue;
  822. for (pos = 0; pos < map->len; pos++)
  823. if (map->queues[pos] == index)
  824. break;
  825. if (pos < map->len) {
  826. if (map->len > 1)
  827. map->queues[pos] =
  828. map->queues[--map->len];
  829. else {
  830. RCU_INIT_POINTER(dev_maps->cpu_map[i],
  831. NULL);
  832. kfree_rcu(map, rcu);
  833. map = NULL;
  834. }
  835. }
  836. if (map)
  837. nonempty = 1;
  838. }
  839. if (!nonempty) {
  840. RCU_INIT_POINTER(dev->xps_maps, NULL);
  841. kfree_rcu(dev_maps, rcu);
  842. }
  843. }
  844. mutex_unlock(&xps_map_mutex);
  845. }
  846. static ssize_t store_xps_map(struct netdev_queue *queue,
  847. struct netdev_queue_attribute *attribute,
  848. const char *buf, size_t len)
  849. {
  850. struct net_device *dev = queue->dev;
  851. cpumask_var_t mask;
  852. int err, i, cpu, pos, map_len, alloc_len, need_set;
  853. unsigned long index;
  854. struct xps_map *map, *new_map;
  855. struct xps_dev_maps *dev_maps, *new_dev_maps;
  856. int nonempty = 0;
  857. int numa_node_id = -2;
  858. if (!capable(CAP_NET_ADMIN))
  859. return -EPERM;
  860. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  861. return -ENOMEM;
  862. index = get_netdev_queue_index(queue);
  863. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  864. if (err) {
  865. free_cpumask_var(mask);
  866. return err;
  867. }
  868. new_dev_maps = kzalloc(max_t(unsigned int,
  869. XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES), GFP_KERNEL);
  870. if (!new_dev_maps) {
  871. free_cpumask_var(mask);
  872. return -ENOMEM;
  873. }
  874. mutex_lock(&xps_map_mutex);
  875. dev_maps = xmap_dereference(dev->xps_maps);
  876. for_each_possible_cpu(cpu) {
  877. map = dev_maps ?
  878. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  879. new_map = map;
  880. if (map) {
  881. for (pos = 0; pos < map->len; pos++)
  882. if (map->queues[pos] == index)
  883. break;
  884. map_len = map->len;
  885. alloc_len = map->alloc_len;
  886. } else
  887. pos = map_len = alloc_len = 0;
  888. need_set = cpumask_test_cpu(cpu, mask) && cpu_online(cpu);
  889. #ifdef CONFIG_NUMA
  890. if (need_set) {
  891. if (numa_node_id == -2)
  892. numa_node_id = cpu_to_node(cpu);
  893. else if (numa_node_id != cpu_to_node(cpu))
  894. numa_node_id = -1;
  895. }
  896. #endif
  897. if (need_set && pos >= map_len) {
  898. /* Need to add queue to this CPU's map */
  899. if (map_len >= alloc_len) {
  900. alloc_len = alloc_len ?
  901. 2 * alloc_len : XPS_MIN_MAP_ALLOC;
  902. new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len),
  903. GFP_KERNEL,
  904. cpu_to_node(cpu));
  905. if (!new_map)
  906. goto error;
  907. new_map->alloc_len = alloc_len;
  908. for (i = 0; i < map_len; i++)
  909. new_map->queues[i] = map->queues[i];
  910. new_map->len = map_len;
  911. }
  912. new_map->queues[new_map->len++] = index;
  913. } else if (!need_set && pos < map_len) {
  914. /* Need to remove queue from this CPU's map */
  915. if (map_len > 1)
  916. new_map->queues[pos] =
  917. new_map->queues[--new_map->len];
  918. else
  919. new_map = NULL;
  920. }
  921. RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], new_map);
  922. }
  923. /* Cleanup old maps */
  924. for_each_possible_cpu(cpu) {
  925. map = dev_maps ?
  926. xmap_dereference(dev_maps->cpu_map[cpu]) : NULL;
  927. if (map && xmap_dereference(new_dev_maps->cpu_map[cpu]) != map)
  928. kfree_rcu(map, rcu);
  929. if (new_dev_maps->cpu_map[cpu])
  930. nonempty = 1;
  931. }
  932. if (nonempty) {
  933. rcu_assign_pointer(dev->xps_maps, new_dev_maps);
  934. } else {
  935. kfree(new_dev_maps);
  936. RCU_INIT_POINTER(dev->xps_maps, NULL);
  937. }
  938. if (dev_maps)
  939. kfree_rcu(dev_maps, rcu);
  940. netdev_queue_numa_node_write(queue, (numa_node_id >= 0) ? numa_node_id :
  941. NUMA_NO_NODE);
  942. mutex_unlock(&xps_map_mutex);
  943. free_cpumask_var(mask);
  944. return len;
  945. error:
  946. mutex_unlock(&xps_map_mutex);
  947. if (new_dev_maps)
  948. for_each_possible_cpu(i)
  949. kfree(rcu_dereference_protected(
  950. new_dev_maps->cpu_map[i],
  951. 1));
  952. kfree(new_dev_maps);
  953. free_cpumask_var(mask);
  954. return -ENOMEM;
  955. }
  956. static struct netdev_queue_attribute xps_cpus_attribute =
  957. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  958. #endif /* CONFIG_XPS */
  959. static struct attribute *netdev_queue_default_attrs[] = {
  960. &queue_trans_timeout.attr,
  961. #ifdef CONFIG_XPS
  962. &xps_cpus_attribute.attr,
  963. #endif
  964. NULL
  965. };
  966. static void netdev_queue_release(struct kobject *kobj)
  967. {
  968. struct netdev_queue *queue = to_netdev_queue(kobj);
  969. #ifdef CONFIG_XPS
  970. xps_queue_release(queue);
  971. #endif
  972. memset(kobj, 0, sizeof(*kobj));
  973. dev_put(queue->dev);
  974. }
  975. static struct kobj_type netdev_queue_ktype = {
  976. .sysfs_ops = &netdev_queue_sysfs_ops,
  977. .release = netdev_queue_release,
  978. .default_attrs = netdev_queue_default_attrs,
  979. };
  980. static int netdev_queue_add_kobject(struct net_device *net, int index)
  981. {
  982. struct netdev_queue *queue = net->_tx + index;
  983. struct kobject *kobj = &queue->kobj;
  984. int error = 0;
  985. kobj->kset = net->queues_kset;
  986. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  987. "tx-%u", index);
  988. if (error)
  989. goto exit;
  990. #ifdef CONFIG_BQL
  991. error = sysfs_create_group(kobj, &dql_group);
  992. if (error)
  993. goto exit;
  994. #endif
  995. kobject_uevent(kobj, KOBJ_ADD);
  996. dev_hold(queue->dev);
  997. return 0;
  998. exit:
  999. kobject_put(kobj);
  1000. return error;
  1001. }
  1002. #endif /* CONFIG_SYSFS */
  1003. int
  1004. netdev_queue_update_kobjects(struct net_device *net, int old_num, int new_num)
  1005. {
  1006. #ifdef CONFIG_SYSFS
  1007. int i;
  1008. int error = 0;
  1009. for (i = old_num; i < new_num; i++) {
  1010. error = netdev_queue_add_kobject(net, i);
  1011. if (error) {
  1012. new_num = old_num;
  1013. break;
  1014. }
  1015. }
  1016. while (--i >= new_num) {
  1017. struct netdev_queue *queue = net->_tx + i;
  1018. #ifdef CONFIG_BQL
  1019. sysfs_remove_group(&queue->kobj, &dql_group);
  1020. #endif
  1021. kobject_put(&queue->kobj);
  1022. }
  1023. return error;
  1024. #else
  1025. return 0;
  1026. #endif /* CONFIG_SYSFS */
  1027. }
  1028. static int register_queue_kobjects(struct net_device *net)
  1029. {
  1030. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1031. #ifdef CONFIG_SYSFS
  1032. net->queues_kset = kset_create_and_add("queues",
  1033. NULL, &net->dev.kobj);
  1034. if (!net->queues_kset)
  1035. return -ENOMEM;
  1036. #endif
  1037. #ifdef CONFIG_RPS
  1038. real_rx = net->real_num_rx_queues;
  1039. #endif
  1040. real_tx = net->real_num_tx_queues;
  1041. error = net_rx_queue_update_kobjects(net, 0, real_rx);
  1042. if (error)
  1043. goto error;
  1044. rxq = real_rx;
  1045. error = netdev_queue_update_kobjects(net, 0, real_tx);
  1046. if (error)
  1047. goto error;
  1048. txq = real_tx;
  1049. return 0;
  1050. error:
  1051. netdev_queue_update_kobjects(net, txq, 0);
  1052. net_rx_queue_update_kobjects(net, rxq, 0);
  1053. return error;
  1054. }
  1055. static void remove_queue_kobjects(struct net_device *net)
  1056. {
  1057. int real_rx = 0, real_tx = 0;
  1058. #ifdef CONFIG_RPS
  1059. real_rx = net->real_num_rx_queues;
  1060. #endif
  1061. real_tx = net->real_num_tx_queues;
  1062. net_rx_queue_update_kobjects(net, real_rx, 0);
  1063. netdev_queue_update_kobjects(net, real_tx, 0);
  1064. #ifdef CONFIG_SYSFS
  1065. kset_unregister(net->queues_kset);
  1066. #endif
  1067. }
  1068. static void *net_grab_current_ns(void)
  1069. {
  1070. struct net *ns = current->nsproxy->net_ns;
  1071. #ifdef CONFIG_NET_NS
  1072. if (ns)
  1073. atomic_inc(&ns->passive);
  1074. #endif
  1075. return ns;
  1076. }
  1077. static const void *net_initial_ns(void)
  1078. {
  1079. return &init_net;
  1080. }
  1081. static const void *net_netlink_ns(struct sock *sk)
  1082. {
  1083. return sock_net(sk);
  1084. }
  1085. struct kobj_ns_type_operations net_ns_type_operations = {
  1086. .type = KOBJ_NS_TYPE_NET,
  1087. .grab_current_ns = net_grab_current_ns,
  1088. .netlink_ns = net_netlink_ns,
  1089. .initial_ns = net_initial_ns,
  1090. .drop_ns = net_drop_ns,
  1091. };
  1092. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1093. #ifdef CONFIG_HOTPLUG
  1094. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1095. {
  1096. struct net_device *dev = to_net_dev(d);
  1097. int retval;
  1098. /* pass interface to uevent. */
  1099. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1100. if (retval)
  1101. goto exit;
  1102. /* pass ifindex to uevent.
  1103. * ifindex is useful as it won't change (interface name may change)
  1104. * and is what RtNetlink uses natively. */
  1105. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1106. exit:
  1107. return retval;
  1108. }
  1109. #endif
  1110. /*
  1111. * netdev_release -- destroy and free a dead device.
  1112. * Called when last reference to device kobject is gone.
  1113. */
  1114. static void netdev_release(struct device *d)
  1115. {
  1116. struct net_device *dev = to_net_dev(d);
  1117. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1118. kfree(dev->ifalias);
  1119. kfree((char *)dev - dev->padded);
  1120. }
  1121. static const void *net_namespace(struct device *d)
  1122. {
  1123. struct net_device *dev;
  1124. dev = container_of(d, struct net_device, dev);
  1125. return dev_net(dev);
  1126. }
  1127. static struct class net_class = {
  1128. .name = "net",
  1129. .dev_release = netdev_release,
  1130. #ifdef CONFIG_SYSFS
  1131. .dev_attrs = net_class_attributes,
  1132. #endif /* CONFIG_SYSFS */
  1133. #ifdef CONFIG_HOTPLUG
  1134. .dev_uevent = netdev_uevent,
  1135. #endif
  1136. .ns_type = &net_ns_type_operations,
  1137. .namespace = net_namespace,
  1138. };
  1139. /* Delete sysfs entries but hold kobject reference until after all
  1140. * netdev references are gone.
  1141. */
  1142. void netdev_unregister_kobject(struct net_device * net)
  1143. {
  1144. struct device *dev = &(net->dev);
  1145. kobject_get(&dev->kobj);
  1146. remove_queue_kobjects(net);
  1147. device_del(dev);
  1148. }
  1149. /* Create sysfs entries for network device. */
  1150. int netdev_register_kobject(struct net_device *net)
  1151. {
  1152. struct device *dev = &(net->dev);
  1153. const struct attribute_group **groups = net->sysfs_groups;
  1154. int error = 0;
  1155. device_initialize(dev);
  1156. dev->class = &net_class;
  1157. dev->platform_data = net;
  1158. dev->groups = groups;
  1159. dev_set_name(dev, "%s", net->name);
  1160. #ifdef CONFIG_SYSFS
  1161. /* Allow for a device specific group */
  1162. if (*groups)
  1163. groups++;
  1164. *groups++ = &netstat_group;
  1165. #endif /* CONFIG_SYSFS */
  1166. error = device_add(dev);
  1167. if (error)
  1168. return error;
  1169. error = register_queue_kobjects(net);
  1170. if (error) {
  1171. device_del(dev);
  1172. return error;
  1173. }
  1174. return error;
  1175. }
  1176. int netdev_class_create_file(struct class_attribute *class_attr)
  1177. {
  1178. return class_create_file(&net_class, class_attr);
  1179. }
  1180. EXPORT_SYMBOL(netdev_class_create_file);
  1181. void netdev_class_remove_file(struct class_attribute *class_attr)
  1182. {
  1183. class_remove_file(&net_class, class_attr);
  1184. }
  1185. EXPORT_SYMBOL(netdev_class_remove_file);
  1186. int netdev_kobject_init(void)
  1187. {
  1188. kobj_ns_type_register(&net_ns_type_operations);
  1189. return class_register(&net_class);
  1190. }