net-sysfs.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net-sysfs.c - network device class and attributes
  4. *
  5. * Copyright (c) 2003 Stephen Hemminger <[email protected]>
  6. */
  7. #include <linux/capability.h>
  8. #include <linux/kernel.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sched/isolation.h>
  14. #include <linux/nsproxy.h>
  15. #include <net/sock.h>
  16. #include <net/net_namespace.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/export.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of.h>
  23. #include <linux/of_net.h>
  24. #include <linux/cpu.h>
  25. #include "dev.h"
  26. #include "net-sysfs.h"
  27. #ifdef CONFIG_SYSFS
  28. static const char fmt_hex[] = "%#x\n";
  29. static const char fmt_dec[] = "%d\n";
  30. static const char fmt_ulong[] = "%lu\n";
  31. static const char fmt_u64[] = "%llu\n";
  32. /* Caller holds RTNL or dev_base_lock */
  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 sysfs_emit(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;
  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. ret = (*set)(netdev, new);
  85. if (ret == 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 sysfs_emit(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 sysfs_emit(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. struct net_device *netdev = to_net_dev(dev);
  153. /* The check is also done in change_carrier; this helps returning early
  154. * without hitting the trylock/restart in netdev_store.
  155. */
  156. if (!netdev->netdev_ops->ndo_change_carrier)
  157. return -EOPNOTSUPP;
  158. return netdev_store(dev, attr, buf, len, change_carrier);
  159. }
  160. static ssize_t carrier_show(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct net_device *netdev = to_net_dev(dev);
  164. if (netif_running(netdev))
  165. return sysfs_emit(buf, fmt_dec, !!netif_carrier_ok(netdev));
  166. return -EINVAL;
  167. }
  168. static DEVICE_ATTR_RW(carrier);
  169. static ssize_t speed_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct net_device *netdev = to_net_dev(dev);
  173. int ret = -EINVAL;
  174. /* The check is also done in __ethtool_get_link_ksettings; this helps
  175. * returning early without hitting the trylock/restart below.
  176. */
  177. if (!netdev->ethtool_ops->get_link_ksettings)
  178. return ret;
  179. if (!rtnl_trylock())
  180. return restart_syscall();
  181. if (netif_running(netdev) && netif_device_present(netdev)) {
  182. struct ethtool_link_ksettings cmd;
  183. if (!__ethtool_get_link_ksettings(netdev, &cmd))
  184. ret = sysfs_emit(buf, fmt_dec, cmd.base.speed);
  185. }
  186. rtnl_unlock();
  187. return ret;
  188. }
  189. static DEVICE_ATTR_RO(speed);
  190. static ssize_t duplex_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct net_device *netdev = to_net_dev(dev);
  194. int ret = -EINVAL;
  195. /* The check is also done in __ethtool_get_link_ksettings; this helps
  196. * returning early without hitting the trylock/restart below.
  197. */
  198. if (!netdev->ethtool_ops->get_link_ksettings)
  199. return ret;
  200. if (!rtnl_trylock())
  201. return restart_syscall();
  202. if (netif_running(netdev)) {
  203. struct ethtool_link_ksettings cmd;
  204. if (!__ethtool_get_link_ksettings(netdev, &cmd)) {
  205. const char *duplex;
  206. switch (cmd.base.duplex) {
  207. case DUPLEX_HALF:
  208. duplex = "half";
  209. break;
  210. case DUPLEX_FULL:
  211. duplex = "full";
  212. break;
  213. default:
  214. duplex = "unknown";
  215. break;
  216. }
  217. ret = sysfs_emit(buf, "%s\n", duplex);
  218. }
  219. }
  220. rtnl_unlock();
  221. return ret;
  222. }
  223. static DEVICE_ATTR_RO(duplex);
  224. static ssize_t testing_show(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. struct net_device *netdev = to_net_dev(dev);
  228. if (netif_running(netdev))
  229. return sysfs_emit(buf, fmt_dec, !!netif_testing(netdev));
  230. return -EINVAL;
  231. }
  232. static DEVICE_ATTR_RO(testing);
  233. static ssize_t dormant_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct net_device *netdev = to_net_dev(dev);
  237. if (netif_running(netdev))
  238. return sysfs_emit(buf, fmt_dec, !!netif_dormant(netdev));
  239. return -EINVAL;
  240. }
  241. static DEVICE_ATTR_RO(dormant);
  242. static const char *const operstates[] = {
  243. "unknown",
  244. "notpresent", /* currently unused */
  245. "down",
  246. "lowerlayerdown",
  247. "testing",
  248. "dormant",
  249. "up"
  250. };
  251. static ssize_t operstate_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. const struct net_device *netdev = to_net_dev(dev);
  255. unsigned char operstate;
  256. read_lock(&dev_base_lock);
  257. operstate = netdev->operstate;
  258. if (!netif_running(netdev))
  259. operstate = IF_OPER_DOWN;
  260. read_unlock(&dev_base_lock);
  261. if (operstate >= ARRAY_SIZE(operstates))
  262. return -EINVAL; /* should not happen */
  263. return sysfs_emit(buf, "%s\n", operstates[operstate]);
  264. }
  265. static DEVICE_ATTR_RO(operstate);
  266. static ssize_t carrier_changes_show(struct device *dev,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct net_device *netdev = to_net_dev(dev);
  271. return sysfs_emit(buf, fmt_dec,
  272. atomic_read(&netdev->carrier_up_count) +
  273. atomic_read(&netdev->carrier_down_count));
  274. }
  275. static DEVICE_ATTR_RO(carrier_changes);
  276. static ssize_t carrier_up_count_show(struct device *dev,
  277. struct device_attribute *attr,
  278. char *buf)
  279. {
  280. struct net_device *netdev = to_net_dev(dev);
  281. return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
  282. }
  283. static DEVICE_ATTR_RO(carrier_up_count);
  284. static ssize_t carrier_down_count_show(struct device *dev,
  285. struct device_attribute *attr,
  286. char *buf)
  287. {
  288. struct net_device *netdev = to_net_dev(dev);
  289. return sysfs_emit(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
  290. }
  291. static DEVICE_ATTR_RO(carrier_down_count);
  292. /* read-write attributes */
  293. static int change_mtu(struct net_device *dev, unsigned long new_mtu)
  294. {
  295. return dev_set_mtu(dev, (int)new_mtu);
  296. }
  297. static ssize_t mtu_store(struct device *dev, struct device_attribute *attr,
  298. const char *buf, size_t len)
  299. {
  300. return netdev_store(dev, attr, buf, len, change_mtu);
  301. }
  302. NETDEVICE_SHOW_RW(mtu, fmt_dec);
  303. static int change_flags(struct net_device *dev, unsigned long new_flags)
  304. {
  305. return dev_change_flags(dev, (unsigned int)new_flags, NULL);
  306. }
  307. static ssize_t flags_store(struct device *dev, struct device_attribute *attr,
  308. const char *buf, size_t len)
  309. {
  310. return netdev_store(dev, attr, buf, len, change_flags);
  311. }
  312. NETDEVICE_SHOW_RW(flags, fmt_hex);
  313. static ssize_t tx_queue_len_store(struct device *dev,
  314. struct device_attribute *attr,
  315. const char *buf, size_t len)
  316. {
  317. if (!capable(CAP_NET_ADMIN))
  318. return -EPERM;
  319. return netdev_store(dev, attr, buf, len, dev_change_tx_queue_len);
  320. }
  321. NETDEVICE_SHOW_RW(tx_queue_len, fmt_dec);
  322. static int change_gro_flush_timeout(struct net_device *dev, unsigned long val)
  323. {
  324. WRITE_ONCE(dev->gro_flush_timeout, val);
  325. return 0;
  326. }
  327. static ssize_t gro_flush_timeout_store(struct device *dev,
  328. struct device_attribute *attr,
  329. const char *buf, size_t len)
  330. {
  331. if (!capable(CAP_NET_ADMIN))
  332. return -EPERM;
  333. return netdev_store(dev, attr, buf, len, change_gro_flush_timeout);
  334. }
  335. NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
  336. static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val)
  337. {
  338. WRITE_ONCE(dev->napi_defer_hard_irqs, val);
  339. return 0;
  340. }
  341. static ssize_t napi_defer_hard_irqs_store(struct device *dev,
  342. struct device_attribute *attr,
  343. const char *buf, size_t len)
  344. {
  345. if (!capable(CAP_NET_ADMIN))
  346. return -EPERM;
  347. return netdev_store(dev, attr, buf, len, change_napi_defer_hard_irqs);
  348. }
  349. NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_dec);
  350. static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
  351. const char *buf, size_t len)
  352. {
  353. struct net_device *netdev = to_net_dev(dev);
  354. struct net *net = dev_net(netdev);
  355. size_t count = len;
  356. ssize_t ret = 0;
  357. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  358. return -EPERM;
  359. /* ignore trailing newline */
  360. if (len > 0 && buf[len - 1] == '\n')
  361. --count;
  362. if (!rtnl_trylock())
  363. return restart_syscall();
  364. if (dev_isalive(netdev)) {
  365. ret = dev_set_alias(netdev, buf, count);
  366. if (ret < 0)
  367. goto err;
  368. ret = len;
  369. netdev_state_change(netdev);
  370. }
  371. err:
  372. rtnl_unlock();
  373. return ret;
  374. }
  375. static ssize_t ifalias_show(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. const struct net_device *netdev = to_net_dev(dev);
  379. char tmp[IFALIASZ];
  380. ssize_t ret = 0;
  381. ret = dev_get_alias(netdev, tmp, sizeof(tmp));
  382. if (ret > 0)
  383. ret = sysfs_emit(buf, "%s\n", tmp);
  384. return ret;
  385. }
  386. static DEVICE_ATTR_RW(ifalias);
  387. static int change_group(struct net_device *dev, unsigned long new_group)
  388. {
  389. dev_set_group(dev, (int)new_group);
  390. return 0;
  391. }
  392. static ssize_t group_store(struct device *dev, struct device_attribute *attr,
  393. const char *buf, size_t len)
  394. {
  395. return netdev_store(dev, attr, buf, len, change_group);
  396. }
  397. NETDEVICE_SHOW(group, fmt_dec);
  398. static DEVICE_ATTR(netdev_group, 0644, group_show, group_store);
  399. static int change_proto_down(struct net_device *dev, unsigned long proto_down)
  400. {
  401. return dev_change_proto_down(dev, (bool)proto_down);
  402. }
  403. static ssize_t proto_down_store(struct device *dev,
  404. struct device_attribute *attr,
  405. const char *buf, size_t len)
  406. {
  407. return netdev_store(dev, attr, buf, len, change_proto_down);
  408. }
  409. NETDEVICE_SHOW_RW(proto_down, fmt_dec);
  410. static ssize_t phys_port_id_show(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. struct net_device *netdev = to_net_dev(dev);
  414. ssize_t ret = -EINVAL;
  415. /* The check is also done in dev_get_phys_port_id; this helps returning
  416. * early without hitting the trylock/restart below.
  417. */
  418. if (!netdev->netdev_ops->ndo_get_phys_port_id)
  419. return -EOPNOTSUPP;
  420. if (!rtnl_trylock())
  421. return restart_syscall();
  422. if (dev_isalive(netdev)) {
  423. struct netdev_phys_item_id ppid;
  424. ret = dev_get_phys_port_id(netdev, &ppid);
  425. if (!ret)
  426. ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
  427. }
  428. rtnl_unlock();
  429. return ret;
  430. }
  431. static DEVICE_ATTR_RO(phys_port_id);
  432. static ssize_t phys_port_name_show(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct net_device *netdev = to_net_dev(dev);
  436. ssize_t ret = -EINVAL;
  437. /* The checks are also done in dev_get_phys_port_name; this helps
  438. * returning early without hitting the trylock/restart below.
  439. */
  440. if (!netdev->netdev_ops->ndo_get_phys_port_name &&
  441. !netdev->netdev_ops->ndo_get_devlink_port)
  442. return -EOPNOTSUPP;
  443. if (!rtnl_trylock())
  444. return restart_syscall();
  445. if (dev_isalive(netdev)) {
  446. char name[IFNAMSIZ];
  447. ret = dev_get_phys_port_name(netdev, name, sizeof(name));
  448. if (!ret)
  449. ret = sysfs_emit(buf, "%s\n", name);
  450. }
  451. rtnl_unlock();
  452. return ret;
  453. }
  454. static DEVICE_ATTR_RO(phys_port_name);
  455. static ssize_t phys_switch_id_show(struct device *dev,
  456. struct device_attribute *attr, char *buf)
  457. {
  458. struct net_device *netdev = to_net_dev(dev);
  459. ssize_t ret = -EINVAL;
  460. /* The checks are also done in dev_get_phys_port_name; this helps
  461. * returning early without hitting the trylock/restart below. This works
  462. * because recurse is false when calling dev_get_port_parent_id.
  463. */
  464. if (!netdev->netdev_ops->ndo_get_port_parent_id &&
  465. !netdev->netdev_ops->ndo_get_devlink_port)
  466. return -EOPNOTSUPP;
  467. if (!rtnl_trylock())
  468. return restart_syscall();
  469. if (dev_isalive(netdev)) {
  470. struct netdev_phys_item_id ppid = { };
  471. ret = dev_get_port_parent_id(netdev, &ppid, false);
  472. if (!ret)
  473. ret = sysfs_emit(buf, "%*phN\n", ppid.id_len, ppid.id);
  474. }
  475. rtnl_unlock();
  476. return ret;
  477. }
  478. static DEVICE_ATTR_RO(phys_switch_id);
  479. static ssize_t threaded_show(struct device *dev,
  480. struct device_attribute *attr, char *buf)
  481. {
  482. struct net_device *netdev = to_net_dev(dev);
  483. ssize_t ret = -EINVAL;
  484. if (!rtnl_trylock())
  485. return restart_syscall();
  486. if (dev_isalive(netdev))
  487. ret = sysfs_emit(buf, fmt_dec, netdev->threaded);
  488. rtnl_unlock();
  489. return ret;
  490. }
  491. static int modify_napi_threaded(struct net_device *dev, unsigned long val)
  492. {
  493. int ret;
  494. if (list_empty(&dev->napi_list))
  495. return -EOPNOTSUPP;
  496. if (val != 0 && val != 1)
  497. return -EOPNOTSUPP;
  498. ret = dev_set_threaded(dev, val);
  499. return ret;
  500. }
  501. static ssize_t threaded_store(struct device *dev,
  502. struct device_attribute *attr,
  503. const char *buf, size_t len)
  504. {
  505. return netdev_store(dev, attr, buf, len, modify_napi_threaded);
  506. }
  507. static DEVICE_ATTR_RW(threaded);
  508. static struct attribute *net_class_attrs[] __ro_after_init = {
  509. &dev_attr_netdev_group.attr,
  510. &dev_attr_type.attr,
  511. &dev_attr_dev_id.attr,
  512. &dev_attr_dev_port.attr,
  513. &dev_attr_iflink.attr,
  514. &dev_attr_ifindex.attr,
  515. &dev_attr_name_assign_type.attr,
  516. &dev_attr_addr_assign_type.attr,
  517. &dev_attr_addr_len.attr,
  518. &dev_attr_link_mode.attr,
  519. &dev_attr_address.attr,
  520. &dev_attr_broadcast.attr,
  521. &dev_attr_speed.attr,
  522. &dev_attr_duplex.attr,
  523. &dev_attr_dormant.attr,
  524. &dev_attr_testing.attr,
  525. &dev_attr_operstate.attr,
  526. &dev_attr_carrier_changes.attr,
  527. &dev_attr_ifalias.attr,
  528. &dev_attr_carrier.attr,
  529. &dev_attr_mtu.attr,
  530. &dev_attr_flags.attr,
  531. &dev_attr_tx_queue_len.attr,
  532. &dev_attr_gro_flush_timeout.attr,
  533. &dev_attr_napi_defer_hard_irqs.attr,
  534. &dev_attr_phys_port_id.attr,
  535. &dev_attr_phys_port_name.attr,
  536. &dev_attr_phys_switch_id.attr,
  537. &dev_attr_proto_down.attr,
  538. &dev_attr_carrier_up_count.attr,
  539. &dev_attr_carrier_down_count.attr,
  540. &dev_attr_threaded.attr,
  541. NULL,
  542. };
  543. ATTRIBUTE_GROUPS(net_class);
  544. /* Show a given an attribute in the statistics group */
  545. static ssize_t netstat_show(const struct device *d,
  546. struct device_attribute *attr, char *buf,
  547. unsigned long offset)
  548. {
  549. struct net_device *dev = to_net_dev(d);
  550. ssize_t ret = -EINVAL;
  551. WARN_ON(offset > sizeof(struct rtnl_link_stats64) ||
  552. offset % sizeof(u64) != 0);
  553. read_lock(&dev_base_lock);
  554. if (dev_isalive(dev)) {
  555. struct rtnl_link_stats64 temp;
  556. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  557. ret = sysfs_emit(buf, fmt_u64, *(u64 *)(((u8 *)stats) + offset));
  558. }
  559. read_unlock(&dev_base_lock);
  560. return ret;
  561. }
  562. /* generate a read-only statistics attribute */
  563. #define NETSTAT_ENTRY(name) \
  564. static ssize_t name##_show(struct device *d, \
  565. struct device_attribute *attr, char *buf) \
  566. { \
  567. return netstat_show(d, attr, buf, \
  568. offsetof(struct rtnl_link_stats64, name)); \
  569. } \
  570. static DEVICE_ATTR_RO(name)
  571. NETSTAT_ENTRY(rx_packets);
  572. NETSTAT_ENTRY(tx_packets);
  573. NETSTAT_ENTRY(rx_bytes);
  574. NETSTAT_ENTRY(tx_bytes);
  575. NETSTAT_ENTRY(rx_errors);
  576. NETSTAT_ENTRY(tx_errors);
  577. NETSTAT_ENTRY(rx_dropped);
  578. NETSTAT_ENTRY(tx_dropped);
  579. NETSTAT_ENTRY(multicast);
  580. NETSTAT_ENTRY(collisions);
  581. NETSTAT_ENTRY(rx_length_errors);
  582. NETSTAT_ENTRY(rx_over_errors);
  583. NETSTAT_ENTRY(rx_crc_errors);
  584. NETSTAT_ENTRY(rx_frame_errors);
  585. NETSTAT_ENTRY(rx_fifo_errors);
  586. NETSTAT_ENTRY(rx_missed_errors);
  587. NETSTAT_ENTRY(tx_aborted_errors);
  588. NETSTAT_ENTRY(tx_carrier_errors);
  589. NETSTAT_ENTRY(tx_fifo_errors);
  590. NETSTAT_ENTRY(tx_heartbeat_errors);
  591. NETSTAT_ENTRY(tx_window_errors);
  592. NETSTAT_ENTRY(rx_compressed);
  593. NETSTAT_ENTRY(tx_compressed);
  594. NETSTAT_ENTRY(rx_nohandler);
  595. static struct attribute *netstat_attrs[] __ro_after_init = {
  596. &dev_attr_rx_packets.attr,
  597. &dev_attr_tx_packets.attr,
  598. &dev_attr_rx_bytes.attr,
  599. &dev_attr_tx_bytes.attr,
  600. &dev_attr_rx_errors.attr,
  601. &dev_attr_tx_errors.attr,
  602. &dev_attr_rx_dropped.attr,
  603. &dev_attr_tx_dropped.attr,
  604. &dev_attr_multicast.attr,
  605. &dev_attr_collisions.attr,
  606. &dev_attr_rx_length_errors.attr,
  607. &dev_attr_rx_over_errors.attr,
  608. &dev_attr_rx_crc_errors.attr,
  609. &dev_attr_rx_frame_errors.attr,
  610. &dev_attr_rx_fifo_errors.attr,
  611. &dev_attr_rx_missed_errors.attr,
  612. &dev_attr_tx_aborted_errors.attr,
  613. &dev_attr_tx_carrier_errors.attr,
  614. &dev_attr_tx_fifo_errors.attr,
  615. &dev_attr_tx_heartbeat_errors.attr,
  616. &dev_attr_tx_window_errors.attr,
  617. &dev_attr_rx_compressed.attr,
  618. &dev_attr_tx_compressed.attr,
  619. &dev_attr_rx_nohandler.attr,
  620. NULL
  621. };
  622. static const struct attribute_group netstat_group = {
  623. .name = "statistics",
  624. .attrs = netstat_attrs,
  625. };
  626. static struct attribute *wireless_attrs[] = {
  627. NULL
  628. };
  629. static const struct attribute_group wireless_group = {
  630. .name = "wireless",
  631. .attrs = wireless_attrs,
  632. };
  633. static bool wireless_group_needed(struct net_device *ndev)
  634. {
  635. if (ndev->ieee80211_ptr)
  636. return true;
  637. if (ndev->wireless_handlers)
  638. return true;
  639. return false;
  640. }
  641. #else /* CONFIG_SYSFS */
  642. #define net_class_groups NULL
  643. #endif /* CONFIG_SYSFS */
  644. #ifdef CONFIG_SYSFS
  645. #define to_rx_queue_attr(_attr) \
  646. container_of(_attr, struct rx_queue_attribute, attr)
  647. #define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
  648. static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
  649. char *buf)
  650. {
  651. const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  652. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  653. if (!attribute->show)
  654. return -EIO;
  655. return attribute->show(queue, buf);
  656. }
  657. static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
  658. const char *buf, size_t count)
  659. {
  660. const struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
  661. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  662. if (!attribute->store)
  663. return -EIO;
  664. return attribute->store(queue, buf, count);
  665. }
  666. static const struct sysfs_ops rx_queue_sysfs_ops = {
  667. .show = rx_queue_attr_show,
  668. .store = rx_queue_attr_store,
  669. };
  670. #ifdef CONFIG_RPS
  671. static ssize_t show_rps_map(struct netdev_rx_queue *queue, char *buf)
  672. {
  673. struct rps_map *map;
  674. cpumask_var_t mask;
  675. int i, len;
  676. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  677. return -ENOMEM;
  678. rcu_read_lock();
  679. map = rcu_dereference(queue->rps_map);
  680. if (map)
  681. for (i = 0; i < map->len; i++)
  682. cpumask_set_cpu(map->cpus[i], mask);
  683. len = sysfs_emit(buf, "%*pb\n", cpumask_pr_args(mask));
  684. rcu_read_unlock();
  685. free_cpumask_var(mask);
  686. return len < PAGE_SIZE ? len : -EINVAL;
  687. }
  688. static ssize_t store_rps_map(struct netdev_rx_queue *queue,
  689. const char *buf, size_t len)
  690. {
  691. struct rps_map *old_map, *map;
  692. cpumask_var_t mask;
  693. int err, cpu, i;
  694. static DEFINE_MUTEX(rps_map_mutex);
  695. if (!capable(CAP_NET_ADMIN))
  696. return -EPERM;
  697. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  698. return -ENOMEM;
  699. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  700. if (err) {
  701. free_cpumask_var(mask);
  702. return err;
  703. }
  704. if (!cpumask_empty(mask)) {
  705. cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_DOMAIN));
  706. cpumask_and(mask, mask, housekeeping_cpumask(HK_TYPE_WQ));
  707. if (cpumask_empty(mask)) {
  708. free_cpumask_var(mask);
  709. return -EINVAL;
  710. }
  711. }
  712. map = kzalloc(max_t(unsigned int,
  713. RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
  714. GFP_KERNEL);
  715. if (!map) {
  716. free_cpumask_var(mask);
  717. return -ENOMEM;
  718. }
  719. i = 0;
  720. for_each_cpu_and(cpu, mask, cpu_online_mask)
  721. map->cpus[i++] = cpu;
  722. if (i) {
  723. map->len = i;
  724. } else {
  725. kfree(map);
  726. map = NULL;
  727. }
  728. mutex_lock(&rps_map_mutex);
  729. old_map = rcu_dereference_protected(queue->rps_map,
  730. mutex_is_locked(&rps_map_mutex));
  731. rcu_assign_pointer(queue->rps_map, map);
  732. if (map)
  733. static_branch_inc(&rps_needed);
  734. if (old_map)
  735. static_branch_dec(&rps_needed);
  736. mutex_unlock(&rps_map_mutex);
  737. if (old_map)
  738. kfree_rcu(old_map, rcu);
  739. free_cpumask_var(mask);
  740. return len;
  741. }
  742. static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  743. char *buf)
  744. {
  745. struct rps_dev_flow_table *flow_table;
  746. unsigned long val = 0;
  747. rcu_read_lock();
  748. flow_table = rcu_dereference(queue->rps_flow_table);
  749. if (flow_table)
  750. val = (unsigned long)flow_table->mask + 1;
  751. rcu_read_unlock();
  752. return sysfs_emit(buf, "%lu\n", val);
  753. }
  754. static void rps_dev_flow_table_release(struct rcu_head *rcu)
  755. {
  756. struct rps_dev_flow_table *table = container_of(rcu,
  757. struct rps_dev_flow_table, rcu);
  758. vfree(table);
  759. }
  760. static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
  761. const char *buf, size_t len)
  762. {
  763. unsigned long mask, count;
  764. struct rps_dev_flow_table *table, *old_table;
  765. static DEFINE_SPINLOCK(rps_dev_flow_lock);
  766. int rc;
  767. if (!capable(CAP_NET_ADMIN))
  768. return -EPERM;
  769. rc = kstrtoul(buf, 0, &count);
  770. if (rc < 0)
  771. return rc;
  772. if (count) {
  773. mask = count - 1;
  774. /* mask = roundup_pow_of_two(count) - 1;
  775. * without overflows...
  776. */
  777. while ((mask | (mask >> 1)) != mask)
  778. mask |= (mask >> 1);
  779. /* On 64 bit arches, must check mask fits in table->mask (u32),
  780. * and on 32bit arches, must check
  781. * RPS_DEV_FLOW_TABLE_SIZE(mask + 1) doesn't overflow.
  782. */
  783. #if BITS_PER_LONG > 32
  784. if (mask > (unsigned long)(u32)mask)
  785. return -EINVAL;
  786. #else
  787. if (mask > (ULONG_MAX - RPS_DEV_FLOW_TABLE_SIZE(1))
  788. / sizeof(struct rps_dev_flow)) {
  789. /* Enforce a limit to prevent overflow */
  790. return -EINVAL;
  791. }
  792. #endif
  793. table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(mask + 1));
  794. if (!table)
  795. return -ENOMEM;
  796. table->mask = mask;
  797. for (count = 0; count <= mask; count++)
  798. table->flows[count].cpu = RPS_NO_CPU;
  799. } else {
  800. table = NULL;
  801. }
  802. spin_lock(&rps_dev_flow_lock);
  803. old_table = rcu_dereference_protected(queue->rps_flow_table,
  804. lockdep_is_held(&rps_dev_flow_lock));
  805. rcu_assign_pointer(queue->rps_flow_table, table);
  806. spin_unlock(&rps_dev_flow_lock);
  807. if (old_table)
  808. call_rcu(&old_table->rcu, rps_dev_flow_table_release);
  809. return len;
  810. }
  811. static struct rx_queue_attribute rps_cpus_attribute __ro_after_init
  812. = __ATTR(rps_cpus, 0644, show_rps_map, store_rps_map);
  813. static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute __ro_after_init
  814. = __ATTR(rps_flow_cnt, 0644,
  815. show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
  816. #endif /* CONFIG_RPS */
  817. static struct attribute *rx_queue_default_attrs[] __ro_after_init = {
  818. #ifdef CONFIG_RPS
  819. &rps_cpus_attribute.attr,
  820. &rps_dev_flow_table_cnt_attribute.attr,
  821. #endif
  822. NULL
  823. };
  824. ATTRIBUTE_GROUPS(rx_queue_default);
  825. static void rx_queue_release(struct kobject *kobj)
  826. {
  827. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  828. #ifdef CONFIG_RPS
  829. struct rps_map *map;
  830. struct rps_dev_flow_table *flow_table;
  831. map = rcu_dereference_protected(queue->rps_map, 1);
  832. if (map) {
  833. RCU_INIT_POINTER(queue->rps_map, NULL);
  834. kfree_rcu(map, rcu);
  835. }
  836. flow_table = rcu_dereference_protected(queue->rps_flow_table, 1);
  837. if (flow_table) {
  838. RCU_INIT_POINTER(queue->rps_flow_table, NULL);
  839. call_rcu(&flow_table->rcu, rps_dev_flow_table_release);
  840. }
  841. #endif
  842. memset(kobj, 0, sizeof(*kobj));
  843. netdev_put(queue->dev, &queue->dev_tracker);
  844. }
  845. static const void *rx_queue_namespace(struct kobject *kobj)
  846. {
  847. struct netdev_rx_queue *queue = to_rx_queue(kobj);
  848. struct device *dev = &queue->dev->dev;
  849. const void *ns = NULL;
  850. if (dev->class && dev->class->ns_type)
  851. ns = dev->class->namespace(dev);
  852. return ns;
  853. }
  854. static void rx_queue_get_ownership(struct kobject *kobj,
  855. kuid_t *uid, kgid_t *gid)
  856. {
  857. const struct net *net = rx_queue_namespace(kobj);
  858. net_ns_get_ownership(net, uid, gid);
  859. }
  860. static struct kobj_type rx_queue_ktype __ro_after_init = {
  861. .sysfs_ops = &rx_queue_sysfs_ops,
  862. .release = rx_queue_release,
  863. .default_groups = rx_queue_default_groups,
  864. .namespace = rx_queue_namespace,
  865. .get_ownership = rx_queue_get_ownership,
  866. };
  867. static int rx_queue_add_kobject(struct net_device *dev, int index)
  868. {
  869. struct netdev_rx_queue *queue = dev->_rx + index;
  870. struct kobject *kobj = &queue->kobj;
  871. int error = 0;
  872. /* Kobject_put later will trigger rx_queue_release call which
  873. * decreases dev refcount: Take that reference here
  874. */
  875. netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
  876. kobj->kset = dev->queues_kset;
  877. error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
  878. "rx-%u", index);
  879. if (error)
  880. goto err;
  881. if (dev->sysfs_rx_queue_group) {
  882. error = sysfs_create_group(kobj, dev->sysfs_rx_queue_group);
  883. if (error)
  884. goto err;
  885. }
  886. kobject_uevent(kobj, KOBJ_ADD);
  887. return error;
  888. err:
  889. kobject_put(kobj);
  890. return error;
  891. }
  892. static int rx_queue_change_owner(struct net_device *dev, int index, kuid_t kuid,
  893. kgid_t kgid)
  894. {
  895. struct netdev_rx_queue *queue = dev->_rx + index;
  896. struct kobject *kobj = &queue->kobj;
  897. int error;
  898. error = sysfs_change_owner(kobj, kuid, kgid);
  899. if (error)
  900. return error;
  901. if (dev->sysfs_rx_queue_group)
  902. error = sysfs_group_change_owner(
  903. kobj, dev->sysfs_rx_queue_group, kuid, kgid);
  904. return error;
  905. }
  906. #endif /* CONFIG_SYSFS */
  907. int
  908. net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  909. {
  910. #ifdef CONFIG_SYSFS
  911. int i;
  912. int error = 0;
  913. #ifndef CONFIG_RPS
  914. if (!dev->sysfs_rx_queue_group)
  915. return 0;
  916. #endif
  917. for (i = old_num; i < new_num; i++) {
  918. error = rx_queue_add_kobject(dev, i);
  919. if (error) {
  920. new_num = old_num;
  921. break;
  922. }
  923. }
  924. while (--i >= new_num) {
  925. struct kobject *kobj = &dev->_rx[i].kobj;
  926. if (!refcount_read(&dev_net(dev)->ns.count))
  927. kobj->uevent_suppress = 1;
  928. if (dev->sysfs_rx_queue_group)
  929. sysfs_remove_group(kobj, dev->sysfs_rx_queue_group);
  930. kobject_put(kobj);
  931. }
  932. return error;
  933. #else
  934. return 0;
  935. #endif
  936. }
  937. static int net_rx_queue_change_owner(struct net_device *dev, int num,
  938. kuid_t kuid, kgid_t kgid)
  939. {
  940. #ifdef CONFIG_SYSFS
  941. int error = 0;
  942. int i;
  943. #ifndef CONFIG_RPS
  944. if (!dev->sysfs_rx_queue_group)
  945. return 0;
  946. #endif
  947. for (i = 0; i < num; i++) {
  948. error = rx_queue_change_owner(dev, i, kuid, kgid);
  949. if (error)
  950. break;
  951. }
  952. return error;
  953. #else
  954. return 0;
  955. #endif
  956. }
  957. #ifdef CONFIG_SYSFS
  958. /*
  959. * netdev_queue sysfs structures and functions.
  960. */
  961. struct netdev_queue_attribute {
  962. struct attribute attr;
  963. ssize_t (*show)(struct netdev_queue *queue, char *buf);
  964. ssize_t (*store)(struct netdev_queue *queue,
  965. const char *buf, size_t len);
  966. };
  967. #define to_netdev_queue_attr(_attr) \
  968. container_of(_attr, struct netdev_queue_attribute, attr)
  969. #define to_netdev_queue(obj) container_of(obj, struct netdev_queue, kobj)
  970. static ssize_t netdev_queue_attr_show(struct kobject *kobj,
  971. struct attribute *attr, char *buf)
  972. {
  973. const struct netdev_queue_attribute *attribute
  974. = to_netdev_queue_attr(attr);
  975. struct netdev_queue *queue = to_netdev_queue(kobj);
  976. if (!attribute->show)
  977. return -EIO;
  978. return attribute->show(queue, buf);
  979. }
  980. static ssize_t netdev_queue_attr_store(struct kobject *kobj,
  981. struct attribute *attr,
  982. const char *buf, size_t count)
  983. {
  984. const struct netdev_queue_attribute *attribute
  985. = to_netdev_queue_attr(attr);
  986. struct netdev_queue *queue = to_netdev_queue(kobj);
  987. if (!attribute->store)
  988. return -EIO;
  989. return attribute->store(queue, buf, count);
  990. }
  991. static const struct sysfs_ops netdev_queue_sysfs_ops = {
  992. .show = netdev_queue_attr_show,
  993. .store = netdev_queue_attr_store,
  994. };
  995. static ssize_t tx_timeout_show(struct netdev_queue *queue, char *buf)
  996. {
  997. unsigned long trans_timeout = atomic_long_read(&queue->trans_timeout);
  998. return sysfs_emit(buf, fmt_ulong, trans_timeout);
  999. }
  1000. static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
  1001. {
  1002. struct net_device *dev = queue->dev;
  1003. unsigned int i;
  1004. i = queue - dev->_tx;
  1005. BUG_ON(i >= dev->num_tx_queues);
  1006. return i;
  1007. }
  1008. static ssize_t traffic_class_show(struct netdev_queue *queue,
  1009. char *buf)
  1010. {
  1011. struct net_device *dev = queue->dev;
  1012. int num_tc, tc;
  1013. int index;
  1014. if (!netif_is_multiqueue(dev))
  1015. return -ENOENT;
  1016. if (!rtnl_trylock())
  1017. return restart_syscall();
  1018. index = get_netdev_queue_index(queue);
  1019. /* If queue belongs to subordinate dev use its TC mapping */
  1020. dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
  1021. num_tc = dev->num_tc;
  1022. tc = netdev_txq_to_tc(dev, index);
  1023. rtnl_unlock();
  1024. if (tc < 0)
  1025. return -EINVAL;
  1026. /* We can report the traffic class one of two ways:
  1027. * Subordinate device traffic classes are reported with the traffic
  1028. * class first, and then the subordinate class so for example TC0 on
  1029. * subordinate device 2 will be reported as "0-2". If the queue
  1030. * belongs to the root device it will be reported with just the
  1031. * traffic class, so just "0" for TC 0 for example.
  1032. */
  1033. return num_tc < 0 ? sysfs_emit(buf, "%d%d\n", tc, num_tc) :
  1034. sysfs_emit(buf, "%d\n", tc);
  1035. }
  1036. #ifdef CONFIG_XPS
  1037. static ssize_t tx_maxrate_show(struct netdev_queue *queue,
  1038. char *buf)
  1039. {
  1040. return sysfs_emit(buf, "%lu\n", queue->tx_maxrate);
  1041. }
  1042. static ssize_t tx_maxrate_store(struct netdev_queue *queue,
  1043. const char *buf, size_t len)
  1044. {
  1045. struct net_device *dev = queue->dev;
  1046. int err, index = get_netdev_queue_index(queue);
  1047. u32 rate = 0;
  1048. if (!capable(CAP_NET_ADMIN))
  1049. return -EPERM;
  1050. /* The check is also done later; this helps returning early without
  1051. * hitting the trylock/restart below.
  1052. */
  1053. if (!dev->netdev_ops->ndo_set_tx_maxrate)
  1054. return -EOPNOTSUPP;
  1055. err = kstrtou32(buf, 10, &rate);
  1056. if (err < 0)
  1057. return err;
  1058. if (!rtnl_trylock())
  1059. return restart_syscall();
  1060. err = -EOPNOTSUPP;
  1061. if (dev->netdev_ops->ndo_set_tx_maxrate)
  1062. err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
  1063. rtnl_unlock();
  1064. if (!err) {
  1065. queue->tx_maxrate = rate;
  1066. return len;
  1067. }
  1068. return err;
  1069. }
  1070. static struct netdev_queue_attribute queue_tx_maxrate __ro_after_init
  1071. = __ATTR_RW(tx_maxrate);
  1072. #endif
  1073. static struct netdev_queue_attribute queue_trans_timeout __ro_after_init
  1074. = __ATTR_RO(tx_timeout);
  1075. static struct netdev_queue_attribute queue_traffic_class __ro_after_init
  1076. = __ATTR_RO(traffic_class);
  1077. #ifdef CONFIG_BQL
  1078. /*
  1079. * Byte queue limits sysfs structures and functions.
  1080. */
  1081. static ssize_t bql_show(char *buf, unsigned int value)
  1082. {
  1083. return sysfs_emit(buf, "%u\n", value);
  1084. }
  1085. static ssize_t bql_set(const char *buf, const size_t count,
  1086. unsigned int *pvalue)
  1087. {
  1088. unsigned int value;
  1089. int err;
  1090. if (!strcmp(buf, "max") || !strcmp(buf, "max\n")) {
  1091. value = DQL_MAX_LIMIT;
  1092. } else {
  1093. err = kstrtouint(buf, 10, &value);
  1094. if (err < 0)
  1095. return err;
  1096. if (value > DQL_MAX_LIMIT)
  1097. return -EINVAL;
  1098. }
  1099. *pvalue = value;
  1100. return count;
  1101. }
  1102. static ssize_t bql_show_hold_time(struct netdev_queue *queue,
  1103. char *buf)
  1104. {
  1105. struct dql *dql = &queue->dql;
  1106. return sysfs_emit(buf, "%u\n", jiffies_to_msecs(dql->slack_hold_time));
  1107. }
  1108. static ssize_t bql_set_hold_time(struct netdev_queue *queue,
  1109. const char *buf, size_t len)
  1110. {
  1111. struct dql *dql = &queue->dql;
  1112. unsigned int value;
  1113. int err;
  1114. err = kstrtouint(buf, 10, &value);
  1115. if (err < 0)
  1116. return err;
  1117. dql->slack_hold_time = msecs_to_jiffies(value);
  1118. return len;
  1119. }
  1120. static struct netdev_queue_attribute bql_hold_time_attribute __ro_after_init
  1121. = __ATTR(hold_time, 0644,
  1122. bql_show_hold_time, bql_set_hold_time);
  1123. static ssize_t bql_show_inflight(struct netdev_queue *queue,
  1124. char *buf)
  1125. {
  1126. struct dql *dql = &queue->dql;
  1127. return sysfs_emit(buf, "%u\n", dql->num_queued - dql->num_completed);
  1128. }
  1129. static struct netdev_queue_attribute bql_inflight_attribute __ro_after_init =
  1130. __ATTR(inflight, 0444, bql_show_inflight, NULL);
  1131. #define BQL_ATTR(NAME, FIELD) \
  1132. static ssize_t bql_show_ ## NAME(struct netdev_queue *queue, \
  1133. char *buf) \
  1134. { \
  1135. return bql_show(buf, queue->dql.FIELD); \
  1136. } \
  1137. \
  1138. static ssize_t bql_set_ ## NAME(struct netdev_queue *queue, \
  1139. const char *buf, size_t len) \
  1140. { \
  1141. return bql_set(buf, len, &queue->dql.FIELD); \
  1142. } \
  1143. \
  1144. static struct netdev_queue_attribute bql_ ## NAME ## _attribute __ro_after_init \
  1145. = __ATTR(NAME, 0644, \
  1146. bql_show_ ## NAME, bql_set_ ## NAME)
  1147. BQL_ATTR(limit, limit);
  1148. BQL_ATTR(limit_max, max_limit);
  1149. BQL_ATTR(limit_min, min_limit);
  1150. static struct attribute *dql_attrs[] __ro_after_init = {
  1151. &bql_limit_attribute.attr,
  1152. &bql_limit_max_attribute.attr,
  1153. &bql_limit_min_attribute.attr,
  1154. &bql_hold_time_attribute.attr,
  1155. &bql_inflight_attribute.attr,
  1156. NULL
  1157. };
  1158. static const struct attribute_group dql_group = {
  1159. .name = "byte_queue_limits",
  1160. .attrs = dql_attrs,
  1161. };
  1162. #endif /* CONFIG_BQL */
  1163. #ifdef CONFIG_XPS
  1164. static ssize_t xps_queue_show(struct net_device *dev, unsigned int index,
  1165. int tc, char *buf, enum xps_map_type type)
  1166. {
  1167. struct xps_dev_maps *dev_maps;
  1168. unsigned long *mask;
  1169. unsigned int nr_ids;
  1170. int j, len;
  1171. rcu_read_lock();
  1172. dev_maps = rcu_dereference(dev->xps_maps[type]);
  1173. /* Default to nr_cpu_ids/dev->num_rx_queues and do not just return 0
  1174. * when dev_maps hasn't been allocated yet, to be backward compatible.
  1175. */
  1176. nr_ids = dev_maps ? dev_maps->nr_ids :
  1177. (type == XPS_CPUS ? nr_cpu_ids : dev->num_rx_queues);
  1178. mask = bitmap_zalloc(nr_ids, GFP_NOWAIT);
  1179. if (!mask) {
  1180. rcu_read_unlock();
  1181. return -ENOMEM;
  1182. }
  1183. if (!dev_maps || tc >= dev_maps->num_tc)
  1184. goto out_no_maps;
  1185. for (j = 0; j < nr_ids; j++) {
  1186. int i, tci = j * dev_maps->num_tc + tc;
  1187. struct xps_map *map;
  1188. map = rcu_dereference(dev_maps->attr_map[tci]);
  1189. if (!map)
  1190. continue;
  1191. for (i = map->len; i--;) {
  1192. if (map->queues[i] == index) {
  1193. __set_bit(j, mask);
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. out_no_maps:
  1199. rcu_read_unlock();
  1200. len = bitmap_print_to_pagebuf(false, buf, mask, nr_ids);
  1201. bitmap_free(mask);
  1202. return len < PAGE_SIZE ? len : -EINVAL;
  1203. }
  1204. static ssize_t xps_cpus_show(struct netdev_queue *queue, char *buf)
  1205. {
  1206. struct net_device *dev = queue->dev;
  1207. unsigned int index;
  1208. int len, tc;
  1209. if (!netif_is_multiqueue(dev))
  1210. return -ENOENT;
  1211. index = get_netdev_queue_index(queue);
  1212. if (!rtnl_trylock())
  1213. return restart_syscall();
  1214. /* If queue belongs to subordinate dev use its map */
  1215. dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
  1216. tc = netdev_txq_to_tc(dev, index);
  1217. if (tc < 0) {
  1218. rtnl_unlock();
  1219. return -EINVAL;
  1220. }
  1221. /* Make sure the subordinate device can't be freed */
  1222. get_device(&dev->dev);
  1223. rtnl_unlock();
  1224. len = xps_queue_show(dev, index, tc, buf, XPS_CPUS);
  1225. put_device(&dev->dev);
  1226. return len;
  1227. }
  1228. static ssize_t xps_cpus_store(struct netdev_queue *queue,
  1229. const char *buf, size_t len)
  1230. {
  1231. struct net_device *dev = queue->dev;
  1232. unsigned int index;
  1233. cpumask_var_t mask;
  1234. int err;
  1235. if (!netif_is_multiqueue(dev))
  1236. return -ENOENT;
  1237. if (!capable(CAP_NET_ADMIN))
  1238. return -EPERM;
  1239. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  1240. return -ENOMEM;
  1241. index = get_netdev_queue_index(queue);
  1242. err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
  1243. if (err) {
  1244. free_cpumask_var(mask);
  1245. return err;
  1246. }
  1247. if (!rtnl_trylock()) {
  1248. free_cpumask_var(mask);
  1249. return restart_syscall();
  1250. }
  1251. err = netif_set_xps_queue(dev, mask, index);
  1252. rtnl_unlock();
  1253. free_cpumask_var(mask);
  1254. return err ? : len;
  1255. }
  1256. static struct netdev_queue_attribute xps_cpus_attribute __ro_after_init
  1257. = __ATTR_RW(xps_cpus);
  1258. static ssize_t xps_rxqs_show(struct netdev_queue *queue, char *buf)
  1259. {
  1260. struct net_device *dev = queue->dev;
  1261. unsigned int index;
  1262. int tc;
  1263. index = get_netdev_queue_index(queue);
  1264. if (!rtnl_trylock())
  1265. return restart_syscall();
  1266. tc = netdev_txq_to_tc(dev, index);
  1267. rtnl_unlock();
  1268. if (tc < 0)
  1269. return -EINVAL;
  1270. return xps_queue_show(dev, index, tc, buf, XPS_RXQS);
  1271. }
  1272. static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
  1273. size_t len)
  1274. {
  1275. struct net_device *dev = queue->dev;
  1276. struct net *net = dev_net(dev);
  1277. unsigned long *mask;
  1278. unsigned int index;
  1279. int err;
  1280. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1281. return -EPERM;
  1282. mask = bitmap_zalloc(dev->num_rx_queues, GFP_KERNEL);
  1283. if (!mask)
  1284. return -ENOMEM;
  1285. index = get_netdev_queue_index(queue);
  1286. err = bitmap_parse(buf, len, mask, dev->num_rx_queues);
  1287. if (err) {
  1288. bitmap_free(mask);
  1289. return err;
  1290. }
  1291. if (!rtnl_trylock()) {
  1292. bitmap_free(mask);
  1293. return restart_syscall();
  1294. }
  1295. cpus_read_lock();
  1296. err = __netif_set_xps_queue(dev, mask, index, XPS_RXQS);
  1297. cpus_read_unlock();
  1298. rtnl_unlock();
  1299. bitmap_free(mask);
  1300. return err ? : len;
  1301. }
  1302. static struct netdev_queue_attribute xps_rxqs_attribute __ro_after_init
  1303. = __ATTR_RW(xps_rxqs);
  1304. #endif /* CONFIG_XPS */
  1305. static struct attribute *netdev_queue_default_attrs[] __ro_after_init = {
  1306. &queue_trans_timeout.attr,
  1307. &queue_traffic_class.attr,
  1308. #ifdef CONFIG_XPS
  1309. &xps_cpus_attribute.attr,
  1310. &xps_rxqs_attribute.attr,
  1311. &queue_tx_maxrate.attr,
  1312. #endif
  1313. NULL
  1314. };
  1315. ATTRIBUTE_GROUPS(netdev_queue_default);
  1316. static void netdev_queue_release(struct kobject *kobj)
  1317. {
  1318. struct netdev_queue *queue = to_netdev_queue(kobj);
  1319. memset(kobj, 0, sizeof(*kobj));
  1320. netdev_put(queue->dev, &queue->dev_tracker);
  1321. }
  1322. static const void *netdev_queue_namespace(struct kobject *kobj)
  1323. {
  1324. struct netdev_queue *queue = to_netdev_queue(kobj);
  1325. struct device *dev = &queue->dev->dev;
  1326. const void *ns = NULL;
  1327. if (dev->class && dev->class->ns_type)
  1328. ns = dev->class->namespace(dev);
  1329. return ns;
  1330. }
  1331. static void netdev_queue_get_ownership(struct kobject *kobj,
  1332. kuid_t *uid, kgid_t *gid)
  1333. {
  1334. const struct net *net = netdev_queue_namespace(kobj);
  1335. net_ns_get_ownership(net, uid, gid);
  1336. }
  1337. static struct kobj_type netdev_queue_ktype __ro_after_init = {
  1338. .sysfs_ops = &netdev_queue_sysfs_ops,
  1339. .release = netdev_queue_release,
  1340. .default_groups = netdev_queue_default_groups,
  1341. .namespace = netdev_queue_namespace,
  1342. .get_ownership = netdev_queue_get_ownership,
  1343. };
  1344. static int netdev_queue_add_kobject(struct net_device *dev, int index)
  1345. {
  1346. struct netdev_queue *queue = dev->_tx + index;
  1347. struct kobject *kobj = &queue->kobj;
  1348. int error = 0;
  1349. /* Kobject_put later will trigger netdev_queue_release call
  1350. * which decreases dev refcount: Take that reference here
  1351. */
  1352. netdev_hold(queue->dev, &queue->dev_tracker, GFP_KERNEL);
  1353. kobj->kset = dev->queues_kset;
  1354. error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
  1355. "tx-%u", index);
  1356. if (error)
  1357. goto err;
  1358. #ifdef CONFIG_BQL
  1359. error = sysfs_create_group(kobj, &dql_group);
  1360. if (error)
  1361. goto err;
  1362. #endif
  1363. kobject_uevent(kobj, KOBJ_ADD);
  1364. return 0;
  1365. err:
  1366. kobject_put(kobj);
  1367. return error;
  1368. }
  1369. static int tx_queue_change_owner(struct net_device *ndev, int index,
  1370. kuid_t kuid, kgid_t kgid)
  1371. {
  1372. struct netdev_queue *queue = ndev->_tx + index;
  1373. struct kobject *kobj = &queue->kobj;
  1374. int error;
  1375. error = sysfs_change_owner(kobj, kuid, kgid);
  1376. if (error)
  1377. return error;
  1378. #ifdef CONFIG_BQL
  1379. error = sysfs_group_change_owner(kobj, &dql_group, kuid, kgid);
  1380. #endif
  1381. return error;
  1382. }
  1383. #endif /* CONFIG_SYSFS */
  1384. int
  1385. netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
  1386. {
  1387. #ifdef CONFIG_SYSFS
  1388. int i;
  1389. int error = 0;
  1390. /* Tx queue kobjects are allowed to be updated when a device is being
  1391. * unregistered, but solely to remove queues from qdiscs. Any path
  1392. * adding queues should be fixed.
  1393. */
  1394. WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
  1395. "New queues can't be registered after device unregistration.");
  1396. for (i = old_num; i < new_num; i++) {
  1397. error = netdev_queue_add_kobject(dev, i);
  1398. if (error) {
  1399. new_num = old_num;
  1400. break;
  1401. }
  1402. }
  1403. while (--i >= new_num) {
  1404. struct netdev_queue *queue = dev->_tx + i;
  1405. if (!refcount_read(&dev_net(dev)->ns.count))
  1406. queue->kobj.uevent_suppress = 1;
  1407. #ifdef CONFIG_BQL
  1408. sysfs_remove_group(&queue->kobj, &dql_group);
  1409. #endif
  1410. kobject_put(&queue->kobj);
  1411. }
  1412. return error;
  1413. #else
  1414. return 0;
  1415. #endif /* CONFIG_SYSFS */
  1416. }
  1417. static int net_tx_queue_change_owner(struct net_device *dev, int num,
  1418. kuid_t kuid, kgid_t kgid)
  1419. {
  1420. #ifdef CONFIG_SYSFS
  1421. int error = 0;
  1422. int i;
  1423. for (i = 0; i < num; i++) {
  1424. error = tx_queue_change_owner(dev, i, kuid, kgid);
  1425. if (error)
  1426. break;
  1427. }
  1428. return error;
  1429. #else
  1430. return 0;
  1431. #endif /* CONFIG_SYSFS */
  1432. }
  1433. static int register_queue_kobjects(struct net_device *dev)
  1434. {
  1435. int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0;
  1436. #ifdef CONFIG_SYSFS
  1437. dev->queues_kset = kset_create_and_add("queues",
  1438. NULL, &dev->dev.kobj);
  1439. if (!dev->queues_kset)
  1440. return -ENOMEM;
  1441. real_rx = dev->real_num_rx_queues;
  1442. #endif
  1443. real_tx = dev->real_num_tx_queues;
  1444. error = net_rx_queue_update_kobjects(dev, 0, real_rx);
  1445. if (error)
  1446. goto error;
  1447. rxq = real_rx;
  1448. error = netdev_queue_update_kobjects(dev, 0, real_tx);
  1449. if (error)
  1450. goto error;
  1451. txq = real_tx;
  1452. return 0;
  1453. error:
  1454. netdev_queue_update_kobjects(dev, txq, 0);
  1455. net_rx_queue_update_kobjects(dev, rxq, 0);
  1456. #ifdef CONFIG_SYSFS
  1457. kset_unregister(dev->queues_kset);
  1458. #endif
  1459. return error;
  1460. }
  1461. static int queue_change_owner(struct net_device *ndev, kuid_t kuid, kgid_t kgid)
  1462. {
  1463. int error = 0, real_rx = 0, real_tx = 0;
  1464. #ifdef CONFIG_SYSFS
  1465. if (ndev->queues_kset) {
  1466. error = sysfs_change_owner(&ndev->queues_kset->kobj, kuid, kgid);
  1467. if (error)
  1468. return error;
  1469. }
  1470. real_rx = ndev->real_num_rx_queues;
  1471. #endif
  1472. real_tx = ndev->real_num_tx_queues;
  1473. error = net_rx_queue_change_owner(ndev, real_rx, kuid, kgid);
  1474. if (error)
  1475. return error;
  1476. error = net_tx_queue_change_owner(ndev, real_tx, kuid, kgid);
  1477. if (error)
  1478. return error;
  1479. return 0;
  1480. }
  1481. static void remove_queue_kobjects(struct net_device *dev)
  1482. {
  1483. int real_rx = 0, real_tx = 0;
  1484. #ifdef CONFIG_SYSFS
  1485. real_rx = dev->real_num_rx_queues;
  1486. #endif
  1487. real_tx = dev->real_num_tx_queues;
  1488. net_rx_queue_update_kobjects(dev, real_rx, 0);
  1489. netdev_queue_update_kobjects(dev, real_tx, 0);
  1490. dev->real_num_rx_queues = 0;
  1491. dev->real_num_tx_queues = 0;
  1492. #ifdef CONFIG_SYSFS
  1493. kset_unregister(dev->queues_kset);
  1494. #endif
  1495. }
  1496. static bool net_current_may_mount(void)
  1497. {
  1498. struct net *net = current->nsproxy->net_ns;
  1499. return ns_capable(net->user_ns, CAP_SYS_ADMIN);
  1500. }
  1501. static void *net_grab_current_ns(void)
  1502. {
  1503. struct net *ns = current->nsproxy->net_ns;
  1504. #ifdef CONFIG_NET_NS
  1505. if (ns)
  1506. refcount_inc(&ns->passive);
  1507. #endif
  1508. return ns;
  1509. }
  1510. static const void *net_initial_ns(void)
  1511. {
  1512. return &init_net;
  1513. }
  1514. static const void *net_netlink_ns(struct sock *sk)
  1515. {
  1516. return sock_net(sk);
  1517. }
  1518. const struct kobj_ns_type_operations net_ns_type_operations = {
  1519. .type = KOBJ_NS_TYPE_NET,
  1520. .current_may_mount = net_current_may_mount,
  1521. .grab_current_ns = net_grab_current_ns,
  1522. .netlink_ns = net_netlink_ns,
  1523. .initial_ns = net_initial_ns,
  1524. .drop_ns = net_drop_ns,
  1525. };
  1526. EXPORT_SYMBOL_GPL(net_ns_type_operations);
  1527. static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
  1528. {
  1529. struct net_device *dev = to_net_dev(d);
  1530. int retval;
  1531. /* pass interface to uevent. */
  1532. retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
  1533. if (retval)
  1534. goto exit;
  1535. /* pass ifindex to uevent.
  1536. * ifindex is useful as it won't change (interface name may change)
  1537. * and is what RtNetlink uses natively.
  1538. */
  1539. retval = add_uevent_var(env, "IFINDEX=%d", dev->ifindex);
  1540. exit:
  1541. return retval;
  1542. }
  1543. /*
  1544. * netdev_release -- destroy and free a dead device.
  1545. * Called when last reference to device kobject is gone.
  1546. */
  1547. static void netdev_release(struct device *d)
  1548. {
  1549. struct net_device *dev = to_net_dev(d);
  1550. BUG_ON(dev->reg_state != NETREG_RELEASED);
  1551. /* no need to wait for rcu grace period:
  1552. * device is dead and about to be freed.
  1553. */
  1554. kfree(rcu_access_pointer(dev->ifalias));
  1555. netdev_freemem(dev);
  1556. }
  1557. static const void *net_namespace(struct device *d)
  1558. {
  1559. struct net_device *dev = to_net_dev(d);
  1560. return dev_net(dev);
  1561. }
  1562. static void net_get_ownership(struct device *d, kuid_t *uid, kgid_t *gid)
  1563. {
  1564. struct net_device *dev = to_net_dev(d);
  1565. const struct net *net = dev_net(dev);
  1566. net_ns_get_ownership(net, uid, gid);
  1567. }
  1568. static struct class net_class __ro_after_init = {
  1569. .name = "net",
  1570. .dev_release = netdev_release,
  1571. .dev_groups = net_class_groups,
  1572. .dev_uevent = netdev_uevent,
  1573. .ns_type = &net_ns_type_operations,
  1574. .namespace = net_namespace,
  1575. .get_ownership = net_get_ownership,
  1576. };
  1577. #ifdef CONFIG_OF
  1578. static int of_dev_node_match(struct device *dev, const void *data)
  1579. {
  1580. for (; dev; dev = dev->parent) {
  1581. if (dev->of_node == data)
  1582. return 1;
  1583. }
  1584. return 0;
  1585. }
  1586. /*
  1587. * of_find_net_device_by_node - lookup the net device for the device node
  1588. * @np: OF device node
  1589. *
  1590. * Looks up the net_device structure corresponding with the device node.
  1591. * If successful, returns a pointer to the net_device with the embedded
  1592. * struct device refcount incremented by one, or NULL on failure. The
  1593. * refcount must be dropped when done with the net_device.
  1594. */
  1595. struct net_device *of_find_net_device_by_node(struct device_node *np)
  1596. {
  1597. struct device *dev;
  1598. dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
  1599. if (!dev)
  1600. return NULL;
  1601. return to_net_dev(dev);
  1602. }
  1603. EXPORT_SYMBOL(of_find_net_device_by_node);
  1604. #endif
  1605. /* Delete sysfs entries but hold kobject reference until after all
  1606. * netdev references are gone.
  1607. */
  1608. void netdev_unregister_kobject(struct net_device *ndev)
  1609. {
  1610. struct device *dev = &ndev->dev;
  1611. if (!refcount_read(&dev_net(ndev)->ns.count))
  1612. dev_set_uevent_suppress(dev, 1);
  1613. kobject_get(&dev->kobj);
  1614. remove_queue_kobjects(ndev);
  1615. pm_runtime_set_memalloc_noio(dev, false);
  1616. device_del(dev);
  1617. }
  1618. /* Create sysfs entries for network device. */
  1619. int netdev_register_kobject(struct net_device *ndev)
  1620. {
  1621. struct device *dev = &ndev->dev;
  1622. const struct attribute_group **groups = ndev->sysfs_groups;
  1623. int error = 0;
  1624. device_initialize(dev);
  1625. dev->class = &net_class;
  1626. dev->platform_data = ndev;
  1627. dev->groups = groups;
  1628. dev_set_name(dev, "%s", ndev->name);
  1629. #ifdef CONFIG_SYSFS
  1630. /* Allow for a device specific group */
  1631. if (*groups)
  1632. groups++;
  1633. *groups++ = &netstat_group;
  1634. if (wireless_group_needed(ndev))
  1635. *groups++ = &wireless_group;
  1636. #endif /* CONFIG_SYSFS */
  1637. error = device_add(dev);
  1638. if (error)
  1639. return error;
  1640. error = register_queue_kobjects(ndev);
  1641. if (error) {
  1642. device_del(dev);
  1643. return error;
  1644. }
  1645. pm_runtime_set_memalloc_noio(dev, true);
  1646. return error;
  1647. }
  1648. /* Change owner for sysfs entries when moving network devices across network
  1649. * namespaces owned by different user namespaces.
  1650. */
  1651. int netdev_change_owner(struct net_device *ndev, const struct net *net_old,
  1652. const struct net *net_new)
  1653. {
  1654. kuid_t old_uid = GLOBAL_ROOT_UID, new_uid = GLOBAL_ROOT_UID;
  1655. kgid_t old_gid = GLOBAL_ROOT_GID, new_gid = GLOBAL_ROOT_GID;
  1656. struct device *dev = &ndev->dev;
  1657. int error;
  1658. net_ns_get_ownership(net_old, &old_uid, &old_gid);
  1659. net_ns_get_ownership(net_new, &new_uid, &new_gid);
  1660. /* The network namespace was changed but the owning user namespace is
  1661. * identical so there's no need to change the owner of sysfs entries.
  1662. */
  1663. if (uid_eq(old_uid, new_uid) && gid_eq(old_gid, new_gid))
  1664. return 0;
  1665. error = device_change_owner(dev, new_uid, new_gid);
  1666. if (error)
  1667. return error;
  1668. error = queue_change_owner(ndev, new_uid, new_gid);
  1669. if (error)
  1670. return error;
  1671. return 0;
  1672. }
  1673. int netdev_class_create_file_ns(const struct class_attribute *class_attr,
  1674. const void *ns)
  1675. {
  1676. return class_create_file_ns(&net_class, class_attr, ns);
  1677. }
  1678. EXPORT_SYMBOL(netdev_class_create_file_ns);
  1679. void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
  1680. const void *ns)
  1681. {
  1682. class_remove_file_ns(&net_class, class_attr, ns);
  1683. }
  1684. EXPORT_SYMBOL(netdev_class_remove_file_ns);
  1685. int __init netdev_kobject_init(void)
  1686. {
  1687. kobj_ns_type_register(&net_ns_type_operations);
  1688. return class_register(&net_class);
  1689. }