net-sysfs.c 37 KB

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