net-sysfs.c 34 KB

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