net-sysfs.c 36 KB

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