net-sysfs.c 42 KB

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