net-sysfs.c 51 KB

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