net-sysfs.c 37 KB

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