net-sysfs.c 36 KB

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