net-sysfs.c 59 KB

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