net-sysfs.c 37 KB

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