net-sysfs.c 38 KB

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