net-sysfs.c 34 KB

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