net-sysfs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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_ulong[] = "%lu\n";
  32. static const char fmt_u64[] = "%llu\n";
  33. static inline int dev_isalive(const struct net_device *dev)
  34. {
  35. return dev->reg_state <= NETREG_REGISTERED;
  36. }
  37. /* use same locking rules as GIF* ioctl's */
  38. static ssize_t netdev_show(const struct device *dev,
  39. struct device_attribute *attr, char *buf,
  40. ssize_t (*format)(const struct net_device *, char *))
  41. {
  42. struct net_device *ndev = to_net_dev(dev);
  43. ssize_t ret = -EINVAL;
  44. read_lock(&dev_base_lock);
  45. if (dev_isalive(ndev))
  46. ret = (*format)(ndev, buf);
  47. read_unlock(&dev_base_lock);
  48. return ret;
  49. }
  50. /* generate a show function for simple field */
  51. #define NETDEVICE_SHOW(field, format_string) \
  52. static ssize_t format_##field(const struct net_device *dev, char *buf) \
  53. { \
  54. return sprintf(buf, format_string, dev->field); \
  55. } \
  56. static ssize_t field##_show(struct device *dev, \
  57. struct device_attribute *attr, char *buf) \
  58. { \
  59. return netdev_show(dev, attr, buf, format_##field); \
  60. } \
  61. #define NETDEVICE_SHOW_RO(field, format_string) \
  62. NETDEVICE_SHOW(field, format_string); \
  63. static DEVICE_ATTR_RO(field)
  64. #define NETDEVICE_SHOW_RW(field, format_string) \
  65. NETDEVICE_SHOW(field, format_string); \
  66. static DEVICE_ATTR_RW(field)
  67. /* use same locking and permission rules as SIF* ioctl's */
  68. static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
  69. const char *buf, size_t len,
  70. int (*set)(struct net_device *, unsigned long))
  71. {
  72. struct net_device *netdev = to_net_dev(dev);
  73. struct net *net = dev_net(netdev);
  74. unsigned long new;
  75. int ret = -EINVAL;
  76. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  77. return -EPERM;
  78. ret = kstrtoul(buf, 0, &new);
  79. if (ret)
  80. goto err;
  81. if (!rtnl_trylock())
  82. return restart_syscall();
  83. if (dev_isalive(netdev)) {
  84. if ((ret = (*set)(netdev, new)) == 0)
  85. ret = len;
  86. }
  87. rtnl_unlock();
  88. err:
  89. return ret;
  90. }
  91. NETDEVICE_SHOW_RO(dev_id, fmt_hex);
  92. NETDEVICE_SHOW_RO(dev_port, fmt_dec);
  93. NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
  94. NETDEVICE_SHOW_RO(addr_len, fmt_dec);
  95. NETDEVICE_SHOW_RO(ifindex, fmt_dec);
  96. NETDEVICE_SHOW_RO(type, fmt_dec);
  97. NETDEVICE_SHOW_RO(link_mode, fmt_dec);
  98. static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
  99. char *buf)
  100. {
  101. struct net_device *ndev = to_net_dev(dev);
  102. return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
  103. }
  104. static DEVICE_ATTR_RO(iflink);
  105. static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
  106. {
  107. return sprintf(buf, fmt_dec, dev->name_assign_type);
  108. }
  109. static ssize_t name_assign_type_show(struct device *dev,
  110. struct device_attribute *attr,
  111. char *buf)
  112. {
  113. struct net_device *ndev = to_net_dev(dev);
  114. ssize_t ret = -EINVAL;
  115. if (ndev->name_assign_type != NET_NAME_UNKNOWN)
  116. ret = netdev_show(dev, attr, buf, format_name_assign_type);
  117. return ret;
  118. }
  119. static DEVICE_ATTR_RO(name_assign_type);
  120. /* use same locking rules as GIFHWADDR ioctl's */
  121. static ssize_t address_show(struct device *dev, struct device_attribute *attr,
  122. char *buf)
  123. {
  124. struct net_device *ndev = to_net_dev(dev);
  125. ssize_t ret = -EINVAL;
  126. read_lock(&dev_base_lock);
  127. if (dev_isalive(ndev))
  128. ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
  129. read_unlock(&dev_base_lock);
  130. return ret;
  131. }
  132. static DEVICE_ATTR_RO(address);
  133. static ssize_t broadcast_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct net_device *ndev = to_net_dev(dev);
  137. if (dev_isalive(ndev))
  138. return sysfs_format_mac(buf, ndev->broadcast, ndev->addr_len);
  139. return -EINVAL;
  140. }
  141. static DEVICE_ATTR_RO(broadcast);
  142. static int change_carrier(struct net_device *dev, unsigned long new_carrier)
  143. {
  144. if (!netif_running(dev))
  145. return -EINVAL;
  146. return dev_change_carrier(dev, (bool) new_carrier);
  147. }
  148. static ssize_t carrier_store(struct device *dev, struct device_attribute *attr,
  149. const char *buf, size_t len)
  150. {
  151. return netdev_store(dev, attr, buf, len, change_carrier);
  152. }
  153. static ssize_t carrier_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct net_device *netdev = to_net_dev(dev);
  157. if (netif_running(netdev)) {
  158. return sprintf(buf, fmt_dec, !!netif_carrier_ok(netdev));
  159. }
  160. return -EINVAL;
  161. }
  162. static DEVICE_ATTR_RW(carrier);
  163. static ssize_t speed_show(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct net_device *netdev = to_net_dev(dev);
  167. int ret = -EINVAL;
  168. if (!rtnl_trylock())
  169. return restart_syscall();
  170. if (netif_running(netdev)) {
  171. struct ethtool_cmd cmd;
  172. if (!__ethtool_get_settings(netdev, &cmd))
  173. ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd));
  174. }
  175. rtnl_unlock();
  176. return ret;
  177. }
  178. static DEVICE_ATTR_RO(speed);
  179. static ssize_t duplex_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct net_device *netdev = to_net_dev(dev);
  183. int ret = -EINVAL;
  184. if (!rtnl_trylock())
  185. return restart_syscall();
  186. if (netif_running(netdev)) {
  187. struct ethtool_cmd cmd;
  188. if (!__ethtool_get_settings(netdev, &cmd)) {
  189. const char *duplex;
  190. switch (cmd.duplex) {
  191. case DUPLEX_HALF:
  192. duplex = "half";
  193. break;
  194. case DUPLEX_FULL:
  195. duplex = "full";
  196. break;
  197. default:
  198. duplex = "unknown";
  199. break;
  200. }
  201. ret = sprintf(buf, "%s\n", duplex);
  202. }
  203. }
  204. rtnl_unlock();
  205. return ret;
  206. }
  207. static DEVICE_ATTR_RO(duplex);
  208. static ssize_t dormant_show(struct device *dev,
  209. struct device_attribute *attr, char *buf)
  210. {
  211. struct net_device *netdev = to_net_dev(dev);
  212. if (netif_running(netdev))
  213. return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
  214. return -EINVAL;
  215. }
  216. static DEVICE_ATTR_RO(dormant);
  217. static const char *const operstates[] = {
  218. "unknown",
  219. "notpresent", /* currently unused */
  220. "down",
  221. "lowerlayerdown",
  222. "testing", /* currently unused */
  223. "dormant",
  224. "up"
  225. };
  226. static ssize_t operstate_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. const struct net_device *netdev = to_net_dev(dev);
  230. unsigned char operstate;
  231. read_lock(&dev_base_lock);
  232. operstate = netdev->operstate;
  233. if (!netif_running(netdev))
  234. operstate = IF_OPER_DOWN;
  235. read_unlock(&dev_base_lock);
  236. if (operstate >= ARRAY_SIZE(operstates))
  237. return -EINVAL; /* should not happen */
  238. return sprintf(buf, "%s\n", operstates[operstate]);
  239. }
  240. static DEVICE_ATTR_RO(operstate);
  241. static ssize_t carrier_changes_show(struct device *dev,
  242. struct device_attribute *attr,
  243. char *buf)
  244. {
  245. struct net_device *netdev = to_net_dev(dev);
  246. return sprintf(buf, fmt_dec,
  247. atomic_read(&netdev->carrier_changes));
  248. }
  249. static DEVICE_ATTR_RO(carrier_changes);
  250. /* read-write attributes */
  251. static int change_mtu(struct net_device *dev, unsigned long new_mtu)
  252. {
  253. return dev_set_mtu(dev, (int) new_mtu);
  254. }
  255. static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
  256. const char *buf, size_t len)
  257. {
  258. return netdev_store(dev, attr, buf, len, change_mtu);
  259. }
  260. NETDEVICE_SHOW_RW(mtu, fmt_dec);
  261. static int change_flags(struct net_device *dev, unsigned long new_flags)
  262. {
  263. return dev_change_flags(dev, (unsigned int) new_flags);
  264. }
  265. static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
  266. const char *buf, size_t len)
  267. {
  268. return netdev_store(dev, attr, buf, len, change_flags);
  269. }
  270. NETDEVICE_SHOW_RW(flags, fmt_hex);
  271. static int change_tx_queue_len(struct net_device *dev, unsigned long new_len)
  272. {
  273. dev->tx_queue_len = new_len;
  274. return 0;
  275. }
  276. static ssize_t tx_queue_len_store(struct device *dev,
  277. struct device_attribute *attr,
  278. const char *buf, size_t len)
  279. {
  280. if (!capable(CAP_NET_ADMIN))
  281. return -EPERM;
  282. return netdev_store(dev, attr, buf, len, change_tx_queue_len);
  283. }
  284. NETDEVICE_SHOW_RW(tx_queue_len, fmt_ulong);
  285. static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
  286. {
  287. dev->gro_flush_timeout = val;
  288. return 0;
  289. }
  290. static ssize_t gro_flush_timeout_store(struct device *dev,
  291. struct device_attribute *attr,
  292. const char *buf, size_t len)
  293. {
  294. if (!capable(CAP_NET_ADMIN))
  295. return -EPERM;
  296. return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
  297. }
  298. NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
  299. static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
  300. const char *buf, size_t len)
  301. {
  302. struct net_device *netdev = to_net_dev(dev);
  303. struct net *net = dev_net(netdev);
  304. size_t count = len;
  305. ssize_t ret;
  306. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  307. return -EPERM;
  308. /* ignore trailing newline */
  309. if (len > 0 && buf[len - 1] == '\n')
  310. --count;
  311. if (!rtnl_trylock())
  312. return restart_syscall();
  313. ret = dev_set_alias(netdev, buf, count);
  314. rtnl_unlock();
  315. return ret < 0 ? ret : len;
  316. }
  317. static ssize_t ifalias_show(struct device *dev,
  318. struct device_attribute *attr, char *buf)
  319. {
  320. const struct net_device *netdev = to_net_dev(dev);
  321. ssize_t ret = 0;
  322. if (!rtnl_trylock())
  323. return restart_syscall();
  324. if (netdev->ifalias)
  325. ret = sprintf(buf, "%s\n", netdev->ifalias);
  326. rtnl_unlock();
  327. return ret;
  328. }
  329. static DEVICE_ATTR_RW(ifalias);
  330. static int change_group(struct net_device *dev, unsigned long new_group)
  331. {
  332. dev_set_group(dev, (int) new_group);
  333. return 0;
  334. }
  335. static ssize_t group_store(struct device *dev, struct device_attribute *attr,
  336. const char *buf, size_t len)
  337. {
  338. return netdev_store(dev, attr, buf, len, change_group);
  339. }
  340. NETDEVICE_SHOW(group, fmt_dec);
  341. static DEVICE_ATTR(netdev_group, S_IRUGO | S_IWUSR, group_show, group_store);
  342. static int change_proto_down(struct net_device *dev, unsigned long proto_down)
  343. {
  344. return dev_change_proto_down(dev, (bool) proto_down);
  345. }
  346. static ssize_t proto_down_store(struct device *dev,
  347. struct device_attribute *attr,
  348. const char *buf, size_t len)
  349. {
  350. return netdev_store(dev, attr, buf, len, change_proto_down);
  351. }
  352. NETDEVICE_SHOW_RW(proto_down, fmt_dec);
  353. static ssize_t phys_port_id_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. struct net_device *netdev = to_net_dev(dev);
  357. ssize_t ret = -EINVAL;
  358. if (!rtnl_trylock())
  359. return restart_syscall();
  360. if (dev_isalive(netdev)) {
  361. struct netdev_phys_item_id ppid;
  362. ret = dev_get_phys_port_id(netdev, &ppid);
  363. if (!ret)
  364. ret = sprintf(buf, "%*phN\n", ppid.id_len, ppid.id);
  365. }
  366. rtnl_unlock();
  367. return ret;
  368. }
  369. static DEVICE_ATTR_RO(phys_port_id);
  370. static ssize_t phys_port_name_show(struct device *dev,
  371. struct device_attribute *attr, char *buf)
  372. {
  373. struct net_device *netdev = to_net_dev(dev);
  374. ssize_t ret = -EINVAL;
  375. if (!rtnl_trylock())
  376. return restart_syscall();
  377. if (dev_isalive(netdev)) {
  378. char name[IFNAMSIZ];
  379. ret = dev_get_phys_port_name(netdev, name, sizeof(name));
  380. if (!ret)
  381. ret = sprintf(buf, "%s\n", name);
  382. }
  383. rtnl_unlock();
  384. return ret;
  385. }
  386. static DEVICE_ATTR_RO(phys_port_name);
  387. static ssize_t phys_switch_id_show(struct device *dev,
  388. struct device_attribute *attr, char *buf)
  389. {
  390. struct net_device *netdev = to_net_dev(dev);
  391. ssize_t ret = -EINVAL;
  392. if (!rtnl_trylock())
  393. return restart_syscall();
  394. if (dev_isalive(netdev)) {
  395. struct switchdev_attr attr = {
  396. .orig_dev = netdev,
  397. .id = SWITCHDEV_ATTR_ID_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 unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  842. {
  843. struct net_device *dev = queue->dev;
  844. unsigned int i;
  845. i = queue - dev->_tx;
  846. BUG_ON(i >= dev->num_tx_queues);
  847. return i;
  848. }
  849. static ssize_t show_tx_maxrate(struct netdev_queue *queue,
  850. struct netdev_queue_attribute *attribute,
  851. char *buf)
  852. {
  853. return sprintf(buf, "%lu\n", queue->tx_maxrate);
  854. }
  855. static ssize_t set_tx_maxrate(struct netdev_queue *queue,
  856. struct netdev_queue_attribute *attribute,
  857. const char *buf, size_t len)
  858. {
  859. struct net_device *dev = queue->dev;
  860. int err, index = get_netdev_queue_index(queue);
  861. u32 rate = 0;
  862. err = kstrtou32(buf, 10, &rate);
  863. if (err < 0)
  864. return err;
  865. if (!rtnl_trylock())
  866. return restart_syscall();
  867. err = -EOPNOTSUPP;
  868. if (dev->netdev_ops->ndo_set_tx_maxrate)
  869. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  870. rtnl_unlock();
  871. if (!err) {
  872. queue->tx_maxrate = rate;
  873. return len;
  874. }
  875. return err;
  876. }
  877. static struct netdev_queue_attribute queue_tx_maxrate =
  878. __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
  879. show_tx_maxrate, set_tx_maxrate);
  880. #endif
  881. static struct netdev_queue_attribute queue_trans_timeout =
  882. __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
  883. #ifdef CONFIG_BQL
  884. /*
  885. * Byte queue limits sysfs structures and functions.
  886. */
  887. static ssize_t bql_show(char *buf, unsigned int value)
  888. {
  889. return sprintf(buf, "%u\n", value);
  890. }
  891. static ssize_t bql_set(const char *buf, const size_t count,
  892. unsigned int *pvalue)
  893. {
  894. unsigned int value;
  895. int err;
  896. if (!strcmp(buf, "max") || !strcmp(buf, "max\n"))
  897. value = DQL_MAX_LIMIT;
  898. else {
  899. err = kstrtouint(buf, 10, &value);
  900. if (err < 0)
  901. return err;
  902. if (value > DQL_MAX_LIMIT)
  903. return -EINVAL;
  904. }
  905. *pvalue = value;
  906. return count;
  907. }
  908. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  909. struct netdev_queue_attribute *attr,
  910. char *buf)
  911. {
  912. struct dql *dql = &queue->dql;
  913. return sprintf(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  914. }
  915. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  916. struct netdev_queue_attribute *attribute,
  917. const char *buf, size_t len)
  918. {
  919. struct dql *dql = &queue->dql;
  920. unsigned int value;
  921. int err;
  922. err = kstrtouint(buf, 10, &value);
  923. if (err < 0)
  924. return err;
  925. dql->slack_hold_time = msecs_to_jiffies(value);
  926. return len;
  927. }
  928. static struct netdev_queue_attribute bql_hold_time_attribute =
  929. __ATTR(hold_time, S_IRUGO | S_IWUSR, bql_show_hold_time,
  930. bql_set_hold_time);
  931. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  932. struct netdev_queue_attribute *attr,
  933. char *buf)
  934. {
  935. struct dql *dql = &queue->dql;
  936. return sprintf(buf, "%u\n", dql->num_queued - dql->num_completed);
  937. }
  938. static struct netdev_queue_attribute bql_inflight_attribute =
  939. __ATTR(inflight, S_IRUGO, bql_show_inflight, NULL);
  940. #define BQL_ATTR(NAME, FIELD) \
  941. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  942. struct netdev_queue_attribute *attr, \
  943. char *buf) \
  944. { \
  945. return bql_show(buf, queue->dql.FIELD); \
  946. } \
  947. \
  948. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  949. struct netdev_queue_attribute *attr, \
  950. const char *buf, size_t len) \
  951. { \
  952. return bql_set(buf, len, &queue->dql.FIELD); \
  953. } \
  954. \
  955. static struct netdev_queue_attribute bql_ ## NAME ## _attribute = \
  956. __ATTR(NAME, S_IRUGO | S_IWUSR, bql_show_ ## NAME, \
  957. bql_set_ ## NAME);
  958. BQL_ATTR(limit, limit)
  959. BQL_ATTR(limit_max, max_limit)
  960. BQL_ATTR(limit_min, min_limit)
  961. static struct attribute *dql_attrs[] = {
  962. &bql_limit_attribute.attr,
  963. &bql_limit_max_attribute.attr,
  964. &bql_limit_min_attribute.attr,
  965. &bql_hold_time_attribute.attr,
  966. &bql_inflight_attribute.attr,
  967. NULL
  968. };
  969. static struct attribute_group dql_group = {
  970. .name = "byte_queue_limits",
  971. .attrs = dql_attrs,
  972. };
  973. #endif /* CONFIG_BQL */
  974. #ifdef CONFIG_XPS
  975. static ssize_t show_xps_map(struct netdev_queue *queue,
  976. struct netdev_queue_attribute *attribute, char *buf)
  977. {
  978. struct net_device *dev = queue->dev;
  979. struct xps_dev_maps *dev_maps;
  980. cpumask_var_t mask;
  981. unsigned long index;
  982. int i, len;
  983. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  984. return -ENOMEM;
  985. index = get_netdev_queue_index(queue);
  986. rcu_read_lock();
  987. dev_maps = rcu_dereference(dev->xps_maps);
  988. if (dev_maps) {
  989. for_each_possible_cpu(i) {
  990. struct xps_map *map =
  991. rcu_dereference(dev_maps->cpu_map[i]);
  992. if (map) {
  993. int j;
  994. for (j = 0; j < map->len; j++) {
  995. if (map->queues[j] == index) {
  996. cpumask_set_cpu(i, mask);
  997. break;
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. rcu_read_unlock();
  1004. len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
  1005. free_cpumask_var(mask);
  1006. return len < PAGE_SIZE ? len : -EINVAL;
  1007. }
  1008. static ssize_t store_xps_map(struct netdev_queue *queue,
  1009. struct netdev_queue_attribute *attribute,
  1010. const char *buf, size_t len)
  1011. {
  1012. struct net_device *dev = queue->dev;
  1013. unsigned long index;
  1014. cpumask_var_t mask;
  1015. int err;
  1016. if (!capable(CAP_NET_ADMIN))
  1017. return -EPERM;
  1018. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1019. return -ENOMEM;
  1020. index = get_netdev_queue_index(queue);
  1021. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1022. if (err) {
  1023. free_cpumask_var(mask);
  1024. return err;
  1025. }
  1026. err = netif_set_xps_queue(dev, mask, index);
  1027. free_cpumask_var(mask);
  1028. return err ? : len;
  1029. }
  1030. static struct netdev_queue_attribute xps_cpus_attribute =
  1031. __ATTR(xps_cpus, S_IRUGO | S_IWUSR, show_xps_map, store_xps_map);
  1032. #endif /* CONFIG_XPS */
  1033. static struct attribute *netdev_queue_default_attrs[] = {
  1034. &queue_trans_timeout.attr,
  1035. #ifdef CONFIG_XPS
  1036. &xps_cpus_attribute.attr,
  1037. &queue_tx_maxrate.attr,
  1038. #endif
  1039. NULL
  1040. };
  1041. static void netdev_queue_release(struct kobject *kobj)
  1042. {
  1043. struct netdev_queue *queue = to_netdev_queue(kobj);
  1044. memset(kobj, 0, sizeof(*kobj));
  1045. dev_put(queue->dev);
  1046. }
  1047. static const void *netdev_queue_namespace(struct kobject *kobj)
  1048. {
  1049. struct netdev_queue *queue = to_netdev_queue(kobj);
  1050. struct device *dev = &queue->dev->dev;
  1051. const void *ns = NULL;
  1052. if (dev->class && dev->class->ns_type)
  1053. ns = dev->class->namespace(dev);
  1054. return ns;
  1055. }
  1056. static struct kobj_type netdev_queue_ktype = {
  1057. .sysfs_ops = &netdev_queue_sysfs_ops,
  1058. .release = netdev_queue_release,
  1059. .default_attrs = netdev_queue_default_attrs,
  1060. .namespace = netdev_queue_namespace,
  1061. };
  1062. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1063. {
  1064. struct netdev_queue *queue = dev->_tx + index;
  1065. struct kobject *kobj = &queue->kobj;
  1066. int error = 0;
  1067. kobj->kset = dev->queues_kset;
  1068. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1069. "tx-%u", index);
  1070. if (error)
  1071. goto exit;
  1072. #ifdef CONFIG_BQL
  1073. error = sysfs_create_group(kobj, &dql_group);
  1074. if (error)
  1075. goto exit;
  1076. #endif
  1077. kobject_uevent(kobj, KOBJ_ADD);
  1078. dev_hold(queue->dev);
  1079. return 0;
  1080. exit:
  1081. kobject_put(kobj);
  1082. return error;
  1083. }
  1084. #endif /* CONFIG_SYSFS */
  1085. int
  1086. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1087. {
  1088. #ifdef CONFIG_SYSFS
  1089. int i;
  1090. int error = 0;
  1091. for (i = old_num; i < new_num; i++) {
  1092. error = netdev_queue_add_kobject(dev, i);
  1093. if (error) {
  1094. new_num = old_num;
  1095. break;
  1096. }
  1097. }
  1098. while (--i >= new_num) {
  1099. struct netdev_queue *queue = dev->_tx + i;
  1100. #ifdef CONFIG_BQL
  1101. sysfs_remove_group(&queue->kobj, &dql_group);
  1102. #endif
  1103. kobject_put(&queue->kobj);
  1104. }
  1105. return error;
  1106. #else
  1107. return 0;
  1108. #endif /* CONFIG_SYSFS */
  1109. }
  1110. static int register_queue_kobjects(struct net_device *dev)
  1111. {
  1112. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1113. #ifdef CONFIG_SYSFS
  1114. dev->queues_kset = kset_create_and_add("queues",
  1115. NULL, &dev->dev.kobj);
  1116. if (!dev->queues_kset)
  1117. return -ENOMEM;
  1118. real_rx = dev->real_num_rx_queues;
  1119. #endif
  1120. real_tx = dev->real_num_tx_queues;
  1121. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1122. if (error)
  1123. goto error;
  1124. rxq = real_rx;
  1125. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1126. if (error)
  1127. goto error;
  1128. txq = real_tx;
  1129. return 0;
  1130. error:
  1131. netdev_queue_update_kobjects(dev, txq, 0);
  1132. net_rx_queue_update_kobjects(dev, rxq, 0);
  1133. return error;
  1134. }
  1135. static void remove_queue_kobjects(struct net_device *dev)
  1136. {
  1137. int real_rx = 0, real_tx = 0;
  1138. #ifdef CONFIG_SYSFS
  1139. real_rx = dev->real_num_rx_queues;
  1140. #endif
  1141. real_tx = dev->real_num_tx_queues;
  1142. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1143. netdev_queue_update_kobjects(dev, real_tx, 0);
  1144. #ifdef CONFIG_SYSFS
  1145. kset_unregister(dev->queues_kset);
  1146. #endif
  1147. }
  1148. static bool net_current_may_mount(void)
  1149. {
  1150. struct net *net = current->nsproxy->net_ns;
  1151. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1152. }
  1153. static void *net_grab_current_ns(void)
  1154. {
  1155. struct net *ns = current->nsproxy->net_ns;
  1156. #ifdef CONFIG_NET_NS
  1157. if (ns)
  1158. atomic_inc(&ns->passive);
  1159. #endif
  1160. return ns;
  1161. }
  1162. static const void *net_initial_ns(void)
  1163. {
  1164. return &init_net;
  1165. }
  1166. static const void *net_netlink_ns(struct sock *sk)
  1167. {
  1168. return sock_net(sk);
  1169. }
  1170. struct kobj_ns_type_operations net_ns_type_operations = {
  1171. .type = KOBJ_NS_TYPE_NET,
  1172. .current_may_mount = net_current_may_mount,
  1173. .grab_current_ns = net_grab_current_ns,
  1174. .netlink_ns = net_netlink_ns,
  1175. .initial_ns = net_initial_ns,
  1176. .drop_ns = net_drop_ns,
  1177. };
  1178. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1179. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1180. {
  1181. struct net_device *dev = to_net_dev(d);
  1182. int retval;
  1183. /* pass interface to uevent. */
  1184. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1185. if (retval)
  1186. goto exit;
  1187. /* pass ifindex to uevent.
  1188. * ifindex is useful as it won't change (interface name may change)
  1189. * and is what RtNetlink uses natively. */
  1190. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1191. exit:
  1192. return retval;
  1193. }
  1194. /*
  1195. * netdev_release -- destroy and free a dead device.
  1196. * Called when last reference to device kobject is gone.
  1197. */
  1198. static void netdev_release(struct device *d)
  1199. {
  1200. struct net_device *dev = to_net_dev(d);
  1201. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1202. kfree(dev->ifalias);
  1203. netdev_freemem(dev);
  1204. }
  1205. static const void *net_namespace(struct device *d)
  1206. {
  1207. struct net_device *dev = to_net_dev(d);
  1208. return dev_net(dev);
  1209. }
  1210. static struct class net_class = {
  1211. .name = "net",
  1212. .dev_release = netdev_release,
  1213. .dev_groups = net_class_groups,
  1214. .dev_uevent = netdev_uevent,
  1215. .ns_type = &net_ns_type_operations,
  1216. .namespace = net_namespace,
  1217. };
  1218. #ifdef CONFIG_OF_NET
  1219. static int of_dev_node_match(struct device *dev, const void *data)
  1220. {
  1221. int ret = 0;
  1222. if (dev->parent)
  1223. ret = dev->parent->of_node == data;
  1224. return ret == 0 ? dev->of_node == data : ret;
  1225. }
  1226. /*
  1227. * of_find_net_device_by_node - lookup the net device for the device node
  1228. * @np: OF device node
  1229. *
  1230. * Looks up the net_device structure corresponding with the device node.
  1231. * If successful, returns a pointer to the net_device with the embedded
  1232. * struct device refcount incremented by one, or NULL on failure. The
  1233. * refcount must be dropped when done with the net_device.
  1234. */
  1235. struct net_device *of_find_net_device_by_node(struct device_node *np)
  1236. {
  1237. struct device *dev;
  1238. dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
  1239. if (!dev)
  1240. return NULL;
  1241. return to_net_dev(dev);
  1242. }
  1243. EXPORT_SYMBOL(of_find_net_device_by_node);
  1244. #endif
  1245. /* Delete sysfs entries but hold kobject reference until after all
  1246. * netdev references are gone.
  1247. */
  1248. void netdev_unregister_kobject(struct net_device *ndev)
  1249. {
  1250. struct device *dev = &(ndev->dev);
  1251. kobject_get(&dev->kobj);
  1252. remove_queue_kobjects(ndev);
  1253. pm_runtime_set_memalloc_noio(dev, false);
  1254. device_del(dev);
  1255. }
  1256. /* Create sysfs entries for network device. */
  1257. int netdev_register_kobject(struct net_device *ndev)
  1258. {
  1259. struct device *dev = &(ndev->dev);
  1260. const struct attribute_group **groups = ndev->sysfs_groups;
  1261. int error = 0;
  1262. device_initialize(dev);
  1263. dev->class = &net_class;
  1264. dev->platform_data = ndev;
  1265. dev->groups = groups;
  1266. dev_set_name(dev, "%s", ndev->name);
  1267. #ifdef CONFIG_SYSFS
  1268. /* Allow for a device specific group */
  1269. if (*groups)
  1270. groups++;
  1271. *groups++ = &netstat_group;
  1272. #if IS_ENABLED(CONFIG_WIRELESS_EXT) || IS_ENABLED(CONFIG_CFG80211)
  1273. if (ndev->ieee80211_ptr)
  1274. *groups++ = &wireless_group;
  1275. #if IS_ENABLED(CONFIG_WIRELESS_EXT)
  1276. else if (ndev->wireless_handlers)
  1277. *groups++ = &wireless_group;
  1278. #endif
  1279. #endif
  1280. #endif /* CONFIG_SYSFS */
  1281. error = device_add(dev);
  1282. if (error)
  1283. return error;
  1284. error = register_queue_kobjects(ndev);
  1285. if (error) {
  1286. device_del(dev);
  1287. return error;
  1288. }
  1289. pm_runtime_set_memalloc_noio(dev, true);
  1290. return error;
  1291. }
  1292. int netdev_class_create_file_ns(struct class_attribute *class_attr,
  1293. const void *ns)
  1294. {
  1295. return class_create_file_ns(&net_class, class_attr, ns);
  1296. }
  1297. EXPORT_SYMBOL(netdev_class_create_file_ns);
  1298. void netdev_class_remove_file_ns(struct class_attribute *class_attr,
  1299. const void *ns)
  1300. {
  1301. class_remove_file_ns(&net_class, class_attr, ns);
  1302. }
  1303. EXPORT_SYMBOL(netdev_class_remove_file_ns);
  1304. int __init netdev_kobject_init(void)
  1305. {
  1306. kobj_ns_type_register(&net_ns_type_operations);
  1307. return class_register(&net_class);
  1308. }