net-sysfs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 int change_proto_down(struct net_device *dev, unsigned long proto_down)
  344. {
  345. return dev_change_proto_down(dev, (bool) proto_down);
  346. }
  347. static ssize_t proto_down_store(struct device *dev,
  348. struct device_attribute *attr,
  349. const char *buf, size_t len)
  350. {
  351. return netdev_store(dev, attr, buf, len, change_proto_down);
  352. }
  353. NETDEVICE_SHOW_RW(proto_down, fmt_dec);
  354. static ssize_t phys_port_id_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. struct net_device *netdev = to_net_dev(dev);
  358. ssize_t ret = -EINVAL;
  359. if (!rtnl_trylock())
  360. return restart_syscall();
  361. if (dev_isalive(netdev)) {
  362. struct netdev_phys_item_id ppid;
  363. ret = dev_get_phys_port_id(netdev, &ppid);
  364. if (!ret)
  365. ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
  366. }
  367. rtnl_unlock();
  368. return ret;
  369. }
  370. static DEVICE_ATTR_RO(phys_port_id);
  371. static ssize_t phys_port_name_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct net_device *netdev = to_net_dev(dev);
  375. ssize_t ret = -EINVAL;
  376. if (!rtnl_trylock())
  377. return restart_syscall();
  378. if (dev_isalive(netdev)) {
  379. char name[IFNAMSIZ];
  380. ret = dev_get_phys_port_name(netdev, name, sizeof(name));
  381. if (!ret)
  382. ret = sprintf(buf, "%s\n", name);
  383. }
  384. rtnl_unlock();
  385. return ret;
  386. }
  387. static DEVICE_ATTR_RO(phys_port_name);
  388. static ssize_t phys_switch_id_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. struct net_device *netdev = to_net_dev(dev);
  392. ssize_t ret = -EINVAL;
  393. if (!rtnl_trylock())
  394. return restart_syscall();
  395. if (dev_isalive(netdev)) {
  396. struct switchdev_attr attr = {
  397. .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
  398. .flags = SWITCHDEV_F_NO_RECURSE,
  399. };
  400. ret = switchdev_port_attr_get(netdev, &attr);
  401. if (!ret)
  402. ret = sprintf(buf, "%*phN\n", attr.u.ppid.id_len,
  403. attr.u.ppid.id);
  404. }
  405. rtnl_unlock();
  406. return ret;
  407. }
  408. static DEVICE_ATTR_RO(phys_switch_id);
  409. static struct attribute *net_class_attrs[] = {
  410. &dev_attr_netdev_group.attr,
  411. &dev_attr_type.attr,
  412. &dev_attr_dev_id.attr,
  413. &dev_attr_dev_port.attr,
  414. &dev_attr_iflink.attr,
  415. &dev_attr_ifindex.attr,
  416. &dev_attr_name_assign_type.attr,
  417. &dev_attr_addr_assign_type.attr,
  418. &dev_attr_addr_len.attr,
  419. &dev_attr_link_mode.attr,
  420. &dev_attr_address.attr,
  421. &dev_attr_broadcast.attr,
  422. &dev_attr_speed.attr,
  423. &dev_attr_duplex.attr,
  424. &dev_attr_dormant.attr,
  425. &dev_attr_operstate.attr,
  426. &dev_attr_carrier_changes.attr,
  427. &dev_attr_ifalias.attr,
  428. &dev_attr_carrier.attr,
  429. &dev_attr_mtu.attr,
  430. &dev_attr_flags.attr,
  431. &dev_attr_tx_queue_len.attr,
  432. &dev_attr_gro_flush_timeout.attr,
  433. &dev_attr_phys_port_id.attr,
  434. &dev_attr_phys_port_name.attr,
  435. &dev_attr_phys_switch_id.attr,
  436. &dev_attr_proto_down.attr,
  437. NULL,
  438. };
  439. ATTRIBUTE_GROUPS(net_class);
  440. /* Show a given an attribute in the statistics group */
  441. static ssize_t netstat_show(const struct device *d,
  442. struct device_attribute *attr, char *buf,
  443. unsigned long offset)
  444. {
  445. struct net_device *dev = to_net_dev(d);
  446. ssize_t ret = -EINVAL;
  447. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  448. offset % sizeof(u64) != 0);
  449. read_lock(&dev_base_lock);
  450. if (dev_isalive(dev)) {
  451. struct rtnl_link_stats64 temp;
  452. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  453. ret = sprintf(buf, fmt_u64, *(u64 *)(((u8 *) stats) + offset));
  454. }
  455. read_unlock(&dev_base_lock);
  456. return ret;
  457. }
  458. /* generate a read-only statistics attribute */
  459. #define NETSTAT_ENTRY(name) \
  460. static ssize_t name##_show(struct device *d, \
  461. struct device_attribute *attr, char *buf) \
  462. { \
  463. return netstat_show(d, attr, buf, \
  464. offsetof(struct rtnl_link_stats64, name)); \
  465. } \
  466. static DEVICE_ATTR_RO(name)
  467. NETSTAT_ENTRY(rx_packets);
  468. NETSTAT_ENTRY(tx_packets);
  469. NETSTAT_ENTRY(rx_bytes);
  470. NETSTAT_ENTRY(tx_bytes);
  471. NETSTAT_ENTRY(rx_errors);
  472. NETSTAT_ENTRY(tx_errors);
  473. NETSTAT_ENTRY(rx_dropped);
  474. NETSTAT_ENTRY(tx_dropped);
  475. NETSTAT_ENTRY(multicast);
  476. NETSTAT_ENTRY(collisions);
  477. NETSTAT_ENTRY(rx_length_errors);
  478. NETSTAT_ENTRY(rx_over_errors);
  479. NETSTAT_ENTRY(rx_crc_errors);
  480. NETSTAT_ENTRY(rx_frame_errors);
  481. NETSTAT_ENTRY(rx_fifo_errors);
  482. NETSTAT_ENTRY(rx_missed_errors);
  483. NETSTAT_ENTRY(tx_aborted_errors);
  484. NETSTAT_ENTRY(tx_carrier_errors);
  485. NETSTAT_ENTRY(tx_fifo_errors);
  486. NETSTAT_ENTRY(tx_heartbeat_errors);
  487. NETSTAT_ENTRY(tx_window_errors);
  488. NETSTAT_ENTRY(rx_compressed);
  489. NETSTAT_ENTRY(tx_compressed);
  490. static struct attribute *netstat_attrs[] = {
  491. &dev_attr_rx_packets.attr,
  492. &dev_attr_tx_packets.attr,
  493. &dev_attr_rx_bytes.attr,
  494. &dev_attr_tx_bytes.attr,
  495. &dev_attr_rx_errors.attr,
  496. &dev_attr_tx_errors.attr,
  497. &dev_attr_rx_dropped.attr,
  498. &dev_attr_tx_dropped.attr,
  499. &dev_attr_multicast.attr,
  500. &dev_attr_collisions.attr,
  501. &dev_attr_rx_length_errors.attr,
  502. &dev_attr_rx_over_errors.attr,
  503. &dev_attr_rx_crc_errors.attr,
  504. &dev_attr_rx_frame_errors.attr,
  505. &dev_attr_rx_fifo_errors.attr,
  506. &dev_attr_rx_missed_errors.attr,
  507. &dev_attr_tx_aborted_errors.attr,
  508. &dev_attr_tx_carrier_errors.attr,
  509. &dev_attr_tx_fifo_errors.attr,
  510. &dev_attr_tx_heartbeat_errors.attr,
  511. &dev_attr_tx_window_errors.attr,
  512. &dev_attr_rx_compressed.attr,
  513. &dev_attr_tx_compressed.attr,
  514. NULL
  515. };
  516. static struct attribute_group netstat_group = {
  517. .name = "statistics",
  518. .attrs = netstat_attrs,
  519. };
  520. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  521. static struct attribute *wireless_attrs[] = {
  522. NULL
  523. };
  524. static struct attribute_group wireless_group = {
  525. .name = "wireless",
  526. .attrs = wireless_attrs,
  527. };
  528. #endif
  529. #else /* CONFIG_SYSFS */
  530. #define net_class_groups NULL
  531. #endif /* CONFIG_SYSFS */
  532. #ifdef CONFIG_SYSFS
  533. #define to_rx_queue_attr(_attr) container_of(_attr, \
  534. struct rx_queue_attribute, attr)
  535. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  536. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  537. char *buf)
  538. {
  539. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  540. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  541. if (!attribute->show)
  542. return -EIO;
  543. return attribute->show(queue, attribute, buf);
  544. }
  545. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  546. const char *buf, size_t count)
  547. {
  548. struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  549. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  550. if (!attribute->store)
  551. return -EIO;
  552. return attribute->store(queue, attribute, buf, count);
  553. }
  554. static const struct sysfs_ops rx_queue_sysfs_ops = {
  555. .show = rx_queue_attr_show,
  556. .store = rx_queue_attr_store,
  557. };
  558. #ifdef CONFIG_RPS
  559. static ssize_t show_rps_map(struct netdev_rx_queue *queue,
  560. struct rx_queue_attribute *attribute, char *buf)
  561. {
  562. struct rps_map *map;
  563. cpumask_var_t mask;
  564. int i, len;
  565. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  566. return -ENOMEM;
  567. rcu_read_lock();
  568. map = rcu_dereference(queue->rps_map);
  569. if (map)
  570. for (i = 0; i < map->len; i++)
  571. cpumask_set_cpu(map->cpus[i], mask);
  572. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  573. rcu_read_unlock();
  574. free_cpumask_var(mask);
  575. return len < PAGE_SIZE ? len : -EINVAL;
  576. }
  577. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  578. struct rx_queue_attribute *attribute,
  579. const char *buf, size_t len)
  580. {
  581. struct rps_map *old_map, *map;
  582. cpumask_var_t mask;
  583. int err, cpu, i;
  584. static DEFINE_MUTEX(rps_map_mutex);
  585. if (!capable(CAP_NET_ADMIN))
  586. return -EPERM;
  587. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  588. return -ENOMEM;
  589. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  590. if (err) {
  591. free_cpumask_var(mask);
  592. return err;
  593. }
  594. map = kzalloc(max_t(unsigned int,
  595. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  596. GFP_KERNEL);
  597. if (!map) {
  598. free_cpumask_var(mask);
  599. return -ENOMEM;
  600. }
  601. i = 0;
  602. for_each_cpu_and(cpu, mask, cpu_online_mask)
  603. map->cpus[i++] = cpu;
  604. if (i)
  605. map->len = i;
  606. else {
  607. kfree(map);
  608. map = NULL;
  609. }
  610. mutex_lock(&rps_map_mutex);
  611. old_map = rcu_dereference_protected(queue->rps_map,
  612. mutex_is_locked(&rps_map_mutex));
  613. rcu_assign_pointer(queue->rps_map, map);
  614. if (map)
  615. static_key_slow_inc(&rps_needed);
  616. if (old_map)
  617. static_key_slow_dec(&rps_needed);
  618. mutex_unlock(&rps_map_mutex);
  619. if (old_map)
  620. kfree_rcu(old_map, rcu);
  621. free_cpumask_var(mask);
  622. return len;
  623. }
  624. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  625. struct rx_queue_attribute *attr,
  626. char *buf)
  627. {
  628. struct rps_dev_flow_table *flow_table;
  629. unsigned long val = 0;
  630. rcu_read_lock();
  631. flow_table = rcu_dereference(queue->rps_flow_table);
  632. if (flow_table)
  633. val = (unsigned long)flow_table->mask + 1;
  634. rcu_read_unlock();
  635. return sprintf(buf, "%lu\n", val);
  636. }
  637. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  638. {
  639. struct rps_dev_flow_table *table = container_of(rcu,
  640. struct rps_dev_flow_table, rcu);
  641. vfree(table);
  642. }
  643. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  644. struct rx_queue_attribute *attr,
  645. const char *buf, size_t len)
  646. {
  647. unsigned long mask, count;
  648. struct rps_dev_flow_table *table, *old_table;
  649. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  650. int rc;
  651. if (!capable(CAP_NET_ADMIN))
  652. return -EPERM;
  653. rc = kstrtoul(buf, 0, &count);
  654. if (rc < 0)
  655. return rc;
  656. if (count) {
  657. mask = count - 1;
  658. /* mask = roundup_pow_of_two(count) - 1;
  659. * without overflows...
  660. */
  661. while ((mask | (mask >> 1)) != mask)
  662. mask |= (mask >> 1);
  663. /* On 64 bit arches, must check mask fits in table->mask (u32),
  664. * and on 32bit arches, must check
  665. * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
  666. */
  667. #if BITS_PER_LONG > 32
  668. if (mask > (unsigned long)(u32)mask)
  669. return -EINVAL;
  670. #else
  671. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  672. / sizeof(struct rps_dev_flow)) {
  673. /* Enforce a limit to prevent overflow */
  674. return -EINVAL;
  675. }
  676. #endif
  677. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  678. if (!table)
  679. return -ENOMEM;
  680. table->mask = mask;
  681. for (count = 0; count <= mask; count++)
  682. table->flows[count].cpu = RPS_NO_CPU;
  683. } else
  684. table = NULL;
  685. spin_lock(&rps_dev_flow_lock);
  686. old_table = rcu_dereference_protected(queue->rps_flow_table,
  687. lockdep_is_held(&rps_dev_flow_lock));
  688. rcu_assign_pointer(queue->rps_flow_table, table);
  689. spin_unlock(&rps_dev_flow_lock);
  690. if (old_table)
  691. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  692. return len;
  693. }
  694. static struct rx_queue_attribute rps_cpus_attribute =
  695. __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
  696. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
  697. __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
  698. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  699. #endif /* CONFIG_RPS */
  700. static struct attribute *rx_queue_default_attrs[] = {
  701. #ifdef CONFIG_RPS
  702. &rps_cpus_attribute.attr,
  703. &rps_dev_flow_table_cnt_attribute.attr,
  704. #endif
  705. NULL
  706. };
  707. static void rx_queue_release(struct kobject *kobj)
  708. {
  709. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  710. #ifdef CONFIG_RPS
  711. struct rps_map *map;
  712. struct rps_dev_flow_table *flow_table;
  713. map = rcu_dereference_protected(queue->rps_map, 1);
  714. if (map) {
  715. RCU_INIT_POINTER(queue->rps_map, NULL);
  716. kfree_rcu(map, rcu);
  717. }
  718. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  719. if (flow_table) {
  720. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  721. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  722. }
  723. #endif
  724. memset(kobj, 0, sizeof(*kobj));
  725. dev_put(queue->dev);
  726. }
  727. static const void *rx_queue_namespace(struct kobject *kobj)
  728. {
  729. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  730. struct device *dev = &queue->dev->dev;
  731. const void *ns = NULL;
  732. if (dev->class && dev->class->ns_type)
  733. ns = dev->class->namespace(dev);
  734. return ns;
  735. }
  736. static struct kobj_type rx_queue_ktype = {
  737. .sysfs_ops = &rx_queue_sysfs_ops,
  738. .release = rx_queue_release,
  739. .default_attrs = rx_queue_default_attrs,
  740. .namespace = rx_queue_namespace
  741. };
  742. static int rx_queue_add_kobject(struct net_device *dev, int index)
  743. {
  744. struct netdev_rx_queue *queue = dev->_rx + index;
  745. struct kobject *kobj = &queue->kobj;
  746. int error = 0;
  747. kobj->kset = dev->queues_kset;
  748. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  749. "rx-%u", index);
  750. if (error)
  751. goto exit;
  752. if (dev->sysfs_rx_queue_group) {
  753. error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
  754. if (error)
  755. goto exit;
  756. }
  757. kobject_uevent(kobj, KOBJ_ADD);
  758. dev_hold(queue->dev);
  759. return error;
  760. exit:
  761. kobject_put(kobj);
  762. return error;
  763. }
  764. #endif /* CONFIG_SYSFS */
  765. int
  766. net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  767. {
  768. #ifdef CONFIG_SYSFS
  769. int i;
  770. int error = 0;
  771. #ifndef CONFIG_RPS
  772. if (!dev->sysfs_rx_queue_group)
  773. return 0;
  774. #endif
  775. for (i = old_num; i < new_num; i++) {
  776. error = rx_queue_add_kobject(dev, i);
  777. if (error) {
  778. new_num = old_num;
  779. break;
  780. }
  781. }
  782. while (--i >= new_num) {
  783. if (dev->sysfs_rx_queue_group)
  784. sysfs_remove_group(&dev->_rx[i].kobj,
  785. dev->sysfs_rx_queue_group);
  786. kobject_put(&dev->_rx[i].kobj);
  787. }
  788. return error;
  789. #else
  790. return 0;
  791. #endif
  792. }
  793. #ifdef CONFIG_SYSFS
  794. /*
  795. * netdev_queue sysfs structures and functions.
  796. */
  797. struct netdev_queue_attribute {
  798. struct attribute attr;
  799. ssize_t (*show)(struct netdev_queue *queue,
  800. struct netdev_queue_attribute *attr, char *buf);
  801. ssize_t (*store)(struct netdev_queue *queue,
  802. struct netdev_queue_attribute *attr, const char *buf, size_t len);
  803. };
  804. #define to_netdev_queue_attr(_attr) container_of(_attr, \
  805. struct netdev_queue_attribute, attr)
  806. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  807. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  808. struct attribute *attr, char *buf)
  809. {
  810. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  811. struct netdev_queue *queue = to_netdev_queue(kobj);
  812. if (!attribute->show)
  813. return -EIO;
  814. return attribute->show(queue, attribute, buf);
  815. }
  816. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  817. struct attribute *attr,
  818. const char *buf, size_t count)
  819. {
  820. struct netdev_queue_attribute *attribute = to_netdev_queue_attr(attr);
  821. struct netdev_queue *queue = to_netdev_queue(kobj);
  822. if (!attribute->store)
  823. return -EIO;
  824. return attribute->store(queue, attribute, buf, count);
  825. }
  826. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  827. .show = netdev_queue_attr_show,
  828. .store = netdev_queue_attr_store,
  829. };
  830. static ssize_t show_trans_timeout(struct netdev_queue *queue,
  831. struct netdev_queue_attribute *attribute,
  832. char *buf)
  833. {
  834. unsigned long trans_timeout;
  835. spin_lock_irq(&queue->_xmit_lock);
  836. trans_timeout = queue->trans_timeout;
  837. spin_unlock_irq(&queue->_xmit_lock);
  838. return sprintf(buf, "%lu", trans_timeout);
  839. }
  840. #ifdef CONFIG_XPS
  841. static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  842. {
  843. struct net_device *dev = queue->dev;
  844. int i;
  845. for (i = 0; i < dev->num_tx_queues; i++)
  846. if (queue == &dev->_tx[i])
  847. break;
  848. BUG_ON(i >= dev->num_tx_queues);
  849. return i;
  850. }
  851. static ssize_t show_tx_maxrate(struct netdev_queue *queue,
  852. struct netdev_queue_attribute *attribute,
  853. char *buf)
  854. {
  855. return sprintf(buf, "%lu\n", queue->tx_maxrate);
  856. }
  857. static ssize_t set_tx_maxrate(struct netdev_queue *queue,
  858. struct netdev_queue_attribute *attribute,
  859. const char *buf, size_t len)
  860. {
  861. struct net_device *dev = queue->dev;
  862. int err, index = get_netdev_queue_index(queue);
  863. u32 rate = 0;
  864. err = kstrtou32(buf, 10, &rate);
  865. if (err < 0)
  866. return err;
  867. if (!rtnl_trylock())
  868. return restart_syscall();
  869. err = -EOPNOTSUPP;
  870. if (dev->netdev_ops->ndo_set_tx_maxrate)
  871. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  872. rtnl_unlock();
  873. if (!err) {
  874. queue->tx_maxrate = rate;
  875. return len;
  876. }
  877. return err;
  878. }
  879. static struct netdev_queue_attribute queue_tx_maxrate =
  880. __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
  881. show_tx_maxrate, set_tx_maxrate);
  882. #endif
  883. static struct netdev_queue_attribute queue_trans_timeout =
  884. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  885. #ifdef CONFIG_BQL
  886. /*
  887. * Byte queue limits sysfs structures and functions.
  888. */
  889. static ssize_t bql_show(char *buf, unsigned int value)
  890. {
  891. return sprintf(buf, "%u\n", value);
  892. }
  893. static ssize_t bql_set(const char *buf, const size_t count,
  894. unsigned int *pvalue)
  895. {
  896. unsigned int value;
  897. int err;
  898. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  899. value = DQL_MAX_LIMIT;
  900. else {
  901. err = kstrtouint(buf, 10, &value);
  902. if (err < 0)
  903. return err;
  904. if (value > DQL_MAX_LIMIT)
  905. return -EINVAL;
  906. }
  907. *pvalue = value;
  908. return count;
  909. }
  910. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  911. struct netdev_queue_attribute *attr,
  912. char *buf)
  913. {
  914. struct dql *dql = &queue->dql;
  915. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  916. }
  917. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  918. struct netdev_queue_attribute *attribute,
  919. const char *buf, size_t len)
  920. {
  921. struct dql *dql = &queue->dql;
  922. unsigned int value;
  923. int err;
  924. err = kstrtouint(buf, 10, &value);
  925. if (err < 0)
  926. return err;
  927. dql->slack_hold_time = msecs_to_jiffies(value);
  928. return len;
  929. }
  930. static struct netdev_queue_attribute bql_hold_time_attribute =
  931. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  932. bql_set_hold_time);
  933. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  934. struct netdev_queue_attribute *attr,
  935. char *buf)
  936. {
  937. struct dql *dql = &queue->dql;
  938. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  939. }
  940. static struct netdev_queue_attribute bql_inflight_attribute =
  941. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  942. #define BQL_ATTR(NAME, FIELD) \
  943. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  944. struct netdev_queue_attribute *attr, \
  945. char *buf) \
  946. { \
  947. return bql_show(buf, queue->dql.FIELD); \
  948. } \
  949. \
  950. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  951. struct netdev_queue_attribute *attr, \
  952. const char *buf, size_t len) \
  953. { \
  954. return bql_set(buf, len, &queue->dql.FIELD); \
  955. } \
  956. \
  957. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  958. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  959. bql_set_ ## NAME);
  960. BQL_ATTR(limit, limit)
  961. BQL_ATTR(limit_max, max_limit)
  962. BQL_ATTR(limit_min, min_limit)
  963. static struct attribute *dql_attrs[] = {
  964. &bql_limit_attribute.attr,
  965. &bql_limit_max_attribute.attr,
  966. &bql_limit_min_attribute.attr,
  967. &bql_hold_time_attribute.attr,
  968. &bql_inflight_attribute.attr,
  969. NULL
  970. };
  971. static struct attribute_group dql_group = {
  972. .name = "byte_queue_limits",
  973. .attrs = dql_attrs,
  974. };
  975. #endif /* CONFIG_BQL */
  976. #ifdef CONFIG_XPS
  977. static ssize_t show_xps_map(struct netdev_queue *queue,
  978. struct netdev_queue_attribute *attribute, char *buf)
  979. {
  980. struct net_device *dev = queue->dev;
  981. struct xps_dev_maps *dev_maps;
  982. cpumask_var_t mask;
  983. unsigned long index;
  984. int i, len;
  985. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  986. return -ENOMEM;
  987. index = get_netdev_queue_index(queue);
  988. rcu_read_lock();
  989. dev_maps = rcu_dereference(dev->xps_maps);
  990. if (dev_maps) {
  991. for_each_possible_cpu(i) {
  992. struct xps_map *map =
  993. rcu_dereference(dev_maps->cpu_map[i]);
  994. if (map) {
  995. int j;
  996. for (j = 0; j < map->len; j++) {
  997. if (map->queues[j] == index) {
  998. cpumask_set_cpu(i, mask);
  999. break;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. rcu_read_unlock();
  1006. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  1007. free_cpumask_var(mask);
  1008. return len < PAGE_SIZE ? len : -EINVAL;
  1009. }
  1010. static ssize_t store_xps_map(struct netdev_queue *queue,
  1011. struct netdev_queue_attribute *attribute,
  1012. const char *buf, size_t len)
  1013. {
  1014. struct net_device *dev = queue->dev;
  1015. unsigned long index;
  1016. cpumask_var_t mask;
  1017. int err;
  1018. if (!capable(CAP_NET_ADMIN))
  1019. return -EPERM;
  1020. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1021. return -ENOMEM;
  1022. index = get_netdev_queue_index(queue);
  1023. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1024. if (err) {
  1025. free_cpumask_var(mask);
  1026. return err;
  1027. }
  1028. err = netif_set_xps_queue(dev, mask, index);
  1029. free_cpumask_var(mask);
  1030. return err ? : len;
  1031. }
  1032. static struct netdev_queue_attribute xps_cpus_attribute =
  1033. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  1034. #endif /* CONFIG_XPS */
  1035. static struct attribute *netdev_queue_default_attrs[] = {
  1036. &queue_trans_timeout.attr,
  1037. #ifdef CONFIG_XPS
  1038. &xps_cpus_attribute.attr,
  1039. &queue_tx_maxrate.attr,
  1040. #endif
  1041. NULL
  1042. };
  1043. static void netdev_queue_release(struct kobject *kobj)
  1044. {
  1045. struct netdev_queue *queue = to_netdev_queue(kobj);
  1046. memset(kobj, 0, sizeof(*kobj));
  1047. dev_put(queue->dev);
  1048. }
  1049. static const void *netdev_queue_namespace(struct kobject *kobj)
  1050. {
  1051. struct netdev_queue *queue = to_netdev_queue(kobj);
  1052. struct device *dev = &queue->dev->dev;
  1053. const void *ns = NULL;
  1054. if (dev->class && dev->class->ns_type)
  1055. ns = dev->class->namespace(dev);
  1056. return ns;
  1057. }
  1058. static struct kobj_type netdev_queue_ktype = {
  1059. .sysfs_ops = &netdev_queue_sysfs_ops,
  1060. .release = netdev_queue_release,
  1061. .default_attrs = netdev_queue_default_attrs,
  1062. .namespace = netdev_queue_namespace,
  1063. };
  1064. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1065. {
  1066. struct netdev_queue *queue = dev->_tx + index;
  1067. struct kobject *kobj = &queue->kobj;
  1068. int error = 0;
  1069. kobj->kset = dev->queues_kset;
  1070. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1071. "tx-%u", index);
  1072. if (error)
  1073. goto exit;
  1074. #ifdef CONFIG_BQL
  1075. error = sysfs_create_group(kobj, &dql_group);
  1076. if (error)
  1077. goto exit;
  1078. #endif
  1079. kobject_uevent(kobj, KOBJ_ADD);
  1080. dev_hold(queue->dev);
  1081. return 0;
  1082. exit:
  1083. kobject_put(kobj);
  1084. return error;
  1085. }
  1086. #endif /* CONFIG_SYSFS */
  1087. int
  1088. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1089. {
  1090. #ifdef CONFIG_SYSFS
  1091. int i;
  1092. int error = 0;
  1093. for (i = old_num; i < new_num; i++) {
  1094. error = netdev_queue_add_kobject(dev, i);
  1095. if (error) {
  1096. new_num = old_num;
  1097. break;
  1098. }
  1099. }
  1100. while (--i >= new_num) {
  1101. struct netdev_queue *queue = dev->_tx + i;
  1102. #ifdef CONFIG_BQL
  1103. sysfs_remove_group(&queue->kobj, &dql_group);
  1104. #endif
  1105. kobject_put(&queue->kobj);
  1106. }
  1107. return error;
  1108. #else
  1109. return 0;
  1110. #endif /* CONFIG_SYSFS */
  1111. }
  1112. static int register_queue_kobjects(struct net_device *dev)
  1113. {
  1114. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1115. #ifdef CONFIG_SYSFS
  1116. dev->queues_kset = kset_create_and_add("queues",
  1117. NULL, &dev->dev.kobj);
  1118. if (!dev->queues_kset)
  1119. return -ENOMEM;
  1120. real_rx = dev->real_num_rx_queues;
  1121. #endif
  1122. real_tx = dev->real_num_tx_queues;
  1123. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1124. if (error)
  1125. goto error;
  1126. rxq = real_rx;
  1127. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1128. if (error)
  1129. goto error;
  1130. txq = real_tx;
  1131. return 0;
  1132. error:
  1133. netdev_queue_update_kobjects(dev, txq, 0);
  1134. net_rx_queue_update_kobjects(dev, rxq, 0);
  1135. return error;
  1136. }
  1137. static void remove_queue_kobjects(struct net_device *dev)
  1138. {
  1139. int real_rx = 0, real_tx = 0;
  1140. #ifdef CONFIG_SYSFS
  1141. real_rx = dev->real_num_rx_queues;
  1142. #endif
  1143. real_tx = dev->real_num_tx_queues;
  1144. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1145. netdev_queue_update_kobjects(dev, real_tx, 0);
  1146. #ifdef CONFIG_SYSFS
  1147. kset_unregister(dev->queues_kset);
  1148. #endif
  1149. }
  1150. static bool net_current_may_mount(void)
  1151. {
  1152. struct net *net = current->nsproxy->net_ns;
  1153. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1154. }
  1155. static void *net_grab_current_ns(void)
  1156. {
  1157. struct net *ns = current->nsproxy->net_ns;
  1158. #ifdef CONFIG_NET_NS
  1159. if (ns)
  1160. atomic_inc(&ns->passive);
  1161. #endif
  1162. return ns;
  1163. }
  1164. static const void *net_initial_ns(void)
  1165. {
  1166. return &init_net;
  1167. }
  1168. static const void *net_netlink_ns(struct sock *sk)
  1169. {
  1170. return sock_net(sk);
  1171. }
  1172. struct kobj_ns_type_operations net_ns_type_operations = {
  1173. .type = KOBJ_NS_TYPE_NET,
  1174. .current_may_mount = net_current_may_mount,
  1175. .grab_current_ns = net_grab_current_ns,
  1176. .netlink_ns = net_netlink_ns,
  1177. .initial_ns = net_initial_ns,
  1178. .drop_ns = net_drop_ns,
  1179. };
  1180. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1181. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1182. {
  1183. struct net_device *dev = to_net_dev(d);
  1184. int retval;
  1185. /* pass interface to uevent. */
  1186. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1187. if (retval)
  1188. goto exit;
  1189. /* pass ifindex to uevent.
  1190. * ifindex is useful as it won't change (interface name may change)
  1191. * and is what RtNetlink uses natively. */
  1192. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1193. exit:
  1194. return retval;
  1195. }
  1196. /*
  1197. * netdev_release -- destroy and free a dead device.
  1198. * Called when last reference to device kobject is gone.
  1199. */
  1200. static void netdev_release(struct device *d)
  1201. {
  1202. struct net_device *dev = to_net_dev(d);
  1203. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1204. kfree(dev->ifalias);
  1205. netdev_freemem(dev);
  1206. }
  1207. static const void *net_namespace(struct device *d)
  1208. {
  1209. struct net_device *dev;
  1210. dev = container_of(d, struct net_device, dev);
  1211. return dev_net(dev);
  1212. }
  1213. static struct class net_class = {
  1214. .name = "net",
  1215. .dev_release = netdev_release,
  1216. .dev_groups = net_class_groups,
  1217. .dev_uevent = netdev_uevent,
  1218. .ns_type = &net_ns_type_operations,
  1219. .namespace = net_namespace,
  1220. };
  1221. #ifdef CONFIG_OF_NET
  1222. static int of_dev_node_match(struct device *dev, const void *data)
  1223. {
  1224. int ret = 0;
  1225. if (dev->parent)
  1226. ret = dev->parent->of_node == data;
  1227. return ret == 0 ? dev->of_node == data : ret;
  1228. }
  1229. struct net_device *of_find_net_device_by_node(struct device_node *np)
  1230. {
  1231. struct device *dev;
  1232. dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
  1233. if (!dev)
  1234. return NULL;
  1235. return to_net_dev(dev);
  1236. }
  1237. EXPORT_SYMBOL(of_find_net_device_by_node);
  1238. #endif
  1239. /* Delete sysfs entries but hold kobject reference until after all
  1240. * netdev references are gone.
  1241. */
  1242. void netdev_unregister_kobject(struct net_device *ndev)
  1243. {
  1244. struct device *dev = &(ndev->dev);
  1245. kobject_get(&dev->kobj);
  1246. remove_queue_kobjects(ndev);
  1247. pm_runtime_set_memalloc_noio(dev, false);
  1248. device_del(dev);
  1249. }
  1250. /* Create sysfs entries for network device. */
  1251. int netdev_register_kobject(struct net_device *ndev)
  1252. {
  1253. struct device *dev = &(ndev->dev);
  1254. const struct attribute_group **groups = ndev->sysfs_groups;
  1255. int error = 0;
  1256. device_initialize(dev);
  1257. dev->class = &net_class;
  1258. dev->platform_data = ndev;
  1259. dev->groups = groups;
  1260. dev_set_name(dev, "%s", ndev->name);
  1261. #ifdef CONFIG_SYSFS
  1262. /* Allow for a device specific group */
  1263. if (*groups)
  1264. groups++;
  1265. *groups++ = &netstat_group;
  1266. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  1267. if (ndev->ieee80211_ptr)
  1268. *groups++ = &wireless_group;
  1269. #if IS_ENABLED(CONFIG_WIRELESS_EXT)
  1270. else if (ndev->wireless_handlers)
  1271. *groups++ = &wireless_group;
  1272. #endif
  1273. #endif
  1274. #endif /* CONFIG_SYSFS */
  1275. error = device_add(dev);
  1276. if (error)
  1277. return error;
  1278. error = register_queue_kobjects(ndev);
  1279. if (error) {
  1280. device_del(dev);
  1281. return error;
  1282. }
  1283. pm_runtime_set_memalloc_noio(dev, true);
  1284. return error;
  1285. }
  1286. int netdev_class_create_file_ns(struct class_attribute *class_attr,
  1287. const void *ns)
  1288. {
  1289. return class_create_file_ns(&net_class, class_attr, ns);
  1290. }
  1291. EXPORT_SYMBOL(netdev_class_create_file_ns);
  1292. void netdev_class_remove_file_ns(struct class_attribute *class_attr,
  1293. const void *ns)
  1294. {
  1295. class_remove_file_ns(&net_class, class_attr, ns);
  1296. }
  1297. EXPORT_SYMBOL(netdev_class_remove_file_ns);
  1298. int __init netdev_kobject_init(void)
  1299. {
  1300. kobj_ns_type_register(&net_ns_type_operations);
  1301. return class_register(&net_class);
  1302. }