net-sysfs.c 34 KB

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