net-sysfs.c 33 KB

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