net-sysfs.c 34 KB

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