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