net-sysfs.c 38 KB

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