net-procfs.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/netdevice.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h>
  5. #include <net/wext.h>
  6. #include <net/hotdata.h>
  7. #include "dev.h"
  8. static void *dev_seq_from_index(struct seq_file *seq, loff_t *pos)
  9. {
  10. unsigned long ifindex = *pos;
  11. struct net_device *dev;
  12. for_each_netdev_dump(seq_file_net(seq), dev, ifindex) {
  13. *pos = dev->ifindex;
  14. return dev;
  15. }
  16. return NULL;
  17. }
  18. static void *dev_seq_start(struct seq_file *seq, loff_t *pos)
  19. __acquires(RCU)
  20. {
  21. rcu_read_lock();
  22. if (!*pos)
  23. return SEQ_START_TOKEN;
  24. return dev_seq_from_index(seq, pos);
  25. }
  26. static void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  27. {
  28. ++*pos;
  29. return dev_seq_from_index(seq, pos);
  30. }
  31. static void dev_seq_stop(struct seq_file *seq, void *v)
  32. __releases(RCU)
  33. {
  34. rcu_read_unlock();
  35. }
  36. static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
  37. {
  38. struct rtnl_link_stats64 temp;
  39. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  40. seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
  41. "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
  42. dev->name, stats->rx_bytes, stats->rx_packets,
  43. stats->rx_errors,
  44. stats->rx_dropped + stats->rx_missed_errors,
  45. stats->rx_fifo_errors,
  46. stats->rx_length_errors + stats->rx_over_errors +
  47. stats->rx_crc_errors + stats->rx_frame_errors,
  48. stats->rx_compressed, stats->multicast,
  49. stats->tx_bytes, stats->tx_packets,
  50. stats->tx_errors, stats->tx_dropped,
  51. stats->tx_fifo_errors, stats->collisions,
  52. stats->tx_carrier_errors +
  53. stats->tx_aborted_errors +
  54. stats->tx_window_errors +
  55. stats->tx_heartbeat_errors,
  56. stats->tx_compressed);
  57. }
  58. /*
  59. * Called from the PROCfs module. This now uses the new arbitrary sized
  60. * /proc/net interface to create /proc/net/dev
  61. */
  62. static int dev_seq_show(struct seq_file *seq, void *v)
  63. {
  64. if (v == SEQ_START_TOKEN)
  65. seq_puts(seq, "Inter-| Receive "
  66. " | Transmit\n"
  67. " face |bytes packets errs drop fifo frame "
  68. "compressed multicast|bytes packets errs "
  69. "drop fifo colls carrier compressed\n");
  70. else
  71. dev_seq_printf_stats(seq, v);
  72. return 0;
  73. }
  74. static u32 softnet_input_pkt_queue_len(struct softnet_data *sd)
  75. {
  76. return skb_queue_len_lockless(&sd->input_pkt_queue);
  77. }
  78. static u32 softnet_process_queue_len(struct softnet_data *sd)
  79. {
  80. return skb_queue_len_lockless(&sd->process_queue);
  81. }
  82. static struct softnet_data *softnet_get_online(loff_t *pos)
  83. {
  84. struct softnet_data *sd = NULL;
  85. while (*pos < nr_cpu_ids)
  86. if (cpu_online(*pos)) {
  87. sd = &per_cpu(softnet_data, *pos);
  88. break;
  89. } else
  90. ++*pos;
  91. return sd;
  92. }
  93. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  94. {
  95. return softnet_get_online(pos);
  96. }
  97. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  98. {
  99. ++*pos;
  100. return softnet_get_online(pos);
  101. }
  102. static void softnet_seq_stop(struct seq_file *seq, void *v)
  103. {
  104. }
  105. static int softnet_seq_show(struct seq_file *seq, void *v)
  106. {
  107. struct softnet_data *sd = v;
  108. u32 input_qlen = softnet_input_pkt_queue_len(sd);
  109. u32 process_qlen = softnet_process_queue_len(sd);
  110. unsigned int flow_limit_count = 0;
  111. #ifdef CONFIG_NET_FLOW_LIMIT
  112. struct sd_flow_limit *fl;
  113. rcu_read_lock();
  114. fl = rcu_dereference(sd->flow_limit);
  115. /* Pairs with WRITE_ONCE() in skb_flow_limit() */
  116. if (fl)
  117. flow_limit_count = READ_ONCE(fl->count);
  118. rcu_read_unlock();
  119. #endif
  120. /* the index is the CPU id owing this sd. Since offline CPUs are not
  121. * displayed, it would be othrwise not trivial for the user-space
  122. * mapping the data a specific CPU
  123. */
  124. seq_printf(seq,
  125. "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x "
  126. "%08x %08x\n",
  127. READ_ONCE(sd->processed),
  128. numa_drop_read(&sd->drop_counters),
  129. READ_ONCE(sd->time_squeeze), 0,
  130. 0, 0, 0, 0, /* was fastroute */
  131. 0, /* was cpu_collision */
  132. READ_ONCE(sd->received_rps), flow_limit_count,
  133. input_qlen + process_qlen, (int)seq->index,
  134. input_qlen, process_qlen);
  135. return 0;
  136. }
  137. static const struct seq_operations dev_seq_ops = {
  138. .start = dev_seq_start,
  139. .next = dev_seq_next,
  140. .stop = dev_seq_stop,
  141. .show = dev_seq_show,
  142. };
  143. static const struct seq_operations softnet_seq_ops = {
  144. .start = softnet_seq_start,
  145. .next = softnet_seq_next,
  146. .stop = softnet_seq_stop,
  147. .show = softnet_seq_show,
  148. };
  149. struct ptype_iter_state {
  150. struct seq_net_private p;
  151. struct net_device *dev;
  152. };
  153. static void *ptype_get_idx(struct seq_file *seq, loff_t pos)
  154. {
  155. struct ptype_iter_state *iter = seq->private;
  156. struct list_head *ptype_list = NULL;
  157. struct packet_type *pt = NULL;
  158. struct net_device *dev;
  159. loff_t i = 0;
  160. int t;
  161. for_each_netdev_rcu(seq_file_net(seq), dev) {
  162. ptype_list = &dev->ptype_all;
  163. list_for_each_entry_rcu(pt, ptype_list, list) {
  164. if (i == pos) {
  165. iter->dev = dev;
  166. return pt;
  167. }
  168. ++i;
  169. }
  170. }
  171. iter->dev = NULL;
  172. list_for_each_entry_rcu(pt, &seq_file_net(seq)->ptype_all, list) {
  173. if (i == pos)
  174. return pt;
  175. ++i;
  176. }
  177. list_for_each_entry_rcu(pt, &seq_file_net(seq)->ptype_specific, list) {
  178. if (i == pos)
  179. return pt;
  180. ++i;
  181. }
  182. for (t = 0; t < PTYPE_HASH_SIZE; t++) {
  183. list_for_each_entry_rcu(pt, &ptype_base[t], list) {
  184. if (i == pos)
  185. return pt;
  186. ++i;
  187. }
  188. }
  189. return NULL;
  190. }
  191. static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
  192. __acquires(RCU)
  193. {
  194. rcu_read_lock();
  195. return *pos ? ptype_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  196. }
  197. static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  198. {
  199. struct ptype_iter_state *iter = seq->private;
  200. struct net *net = seq_file_net(seq);
  201. struct net_device *dev;
  202. struct packet_type *pt;
  203. struct list_head *nxt;
  204. int hash;
  205. ++*pos;
  206. if (v == SEQ_START_TOKEN)
  207. return ptype_get_idx(seq, 0);
  208. pt = v;
  209. nxt = READ_ONCE(pt->list.next);
  210. dev = iter->dev;
  211. if (dev) {
  212. if (nxt != &dev->ptype_all)
  213. goto found;
  214. for_each_netdev_continue_rcu(seq_file_net(seq), dev) {
  215. nxt = READ_ONCE(dev->ptype_all.next);
  216. if (nxt != &dev->ptype_all) {
  217. iter->dev = dev;
  218. goto found;
  219. }
  220. }
  221. iter->dev = NULL;
  222. nxt = READ_ONCE(net->ptype_all.next);
  223. goto net_ptype_all;
  224. }
  225. if (pt->af_packet_net) {
  226. net_ptype_all:
  227. if (nxt != &net->ptype_all && nxt != &net->ptype_specific)
  228. goto found;
  229. if (nxt == &net->ptype_all) {
  230. /* continue with ->ptype_specific if it's not empty */
  231. nxt = READ_ONCE(net->ptype_specific.next);
  232. if (nxt != &net->ptype_specific)
  233. goto found;
  234. }
  235. hash = 0;
  236. nxt = READ_ONCE(ptype_base[0].next);
  237. } else
  238. hash = ntohs(pt->type) & PTYPE_HASH_MASK;
  239. while (nxt == &ptype_base[hash]) {
  240. if (++hash >= PTYPE_HASH_SIZE)
  241. return NULL;
  242. nxt = READ_ONCE(ptype_base[hash].next);
  243. }
  244. found:
  245. return list_entry(nxt, struct packet_type, list);
  246. }
  247. static void ptype_seq_stop(struct seq_file *seq, void *v)
  248. __releases(RCU)
  249. {
  250. rcu_read_unlock();
  251. }
  252. static int ptype_seq_show(struct seq_file *seq, void *v)
  253. {
  254. struct ptype_iter_state *iter = seq->private;
  255. struct packet_type *pt = v;
  256. struct net_device *dev;
  257. if (v == SEQ_START_TOKEN) {
  258. seq_puts(seq, "Type Device Function\n");
  259. return 0;
  260. }
  261. dev = iter->dev;
  262. if ((!pt->af_packet_net || net_eq(pt->af_packet_net, seq_file_net(seq))) &&
  263. (!dev || net_eq(dev_net(dev), seq_file_net(seq)))) {
  264. if (pt->type == htons(ETH_P_ALL))
  265. seq_puts(seq, "ALL ");
  266. else
  267. seq_printf(seq, "%04x", ntohs(pt->type));
  268. seq_printf(seq, " %-8s %ps\n",
  269. dev ? dev->name : "", pt->func);
  270. }
  271. return 0;
  272. }
  273. static const struct seq_operations ptype_seq_ops = {
  274. .start = ptype_seq_start,
  275. .next = ptype_seq_next,
  276. .stop = ptype_seq_stop,
  277. .show = ptype_seq_show,
  278. };
  279. static int __net_init dev_proc_net_init(struct net *net)
  280. {
  281. int rc = -ENOMEM;
  282. if (!proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops,
  283. sizeof(struct seq_net_private)))
  284. goto out;
  285. if (!proc_create_seq("softnet_stat", 0444, net->proc_net,
  286. &softnet_seq_ops))
  287. goto out_dev;
  288. if (!proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  289. sizeof(struct ptype_iter_state)))
  290. goto out_softnet;
  291. if (wext_proc_init(net))
  292. goto out_ptype;
  293. rc = 0;
  294. out:
  295. return rc;
  296. out_ptype:
  297. remove_proc_entry("ptype", net->proc_net);
  298. out_softnet:
  299. remove_proc_entry("softnet_stat", net->proc_net);
  300. out_dev:
  301. remove_proc_entry("dev", net->proc_net);
  302. goto out;
  303. }
  304. static void __net_exit dev_proc_net_exit(struct net *net)
  305. {
  306. wext_proc_exit(net);
  307. remove_proc_entry("ptype", net->proc_net);
  308. remove_proc_entry("softnet_stat", net->proc_net);
  309. remove_proc_entry("dev", net->proc_net);
  310. }
  311. static struct pernet_operations __net_initdata dev_proc_ops = {
  312. .init = dev_proc_net_init,
  313. .exit = dev_proc_net_exit,
  314. };
  315. static int dev_mc_seq_show(struct seq_file *seq, void *v)
  316. {
  317. struct netdev_hw_addr *ha;
  318. struct net_device *dev = v;
  319. if (v == SEQ_START_TOKEN)
  320. return 0;
  321. netif_addr_lock_bh(dev);
  322. netdev_for_each_mc_addr(ha, dev) {
  323. seq_printf(seq, "%-4d %-15s %-5d %-5d %*phN\n",
  324. dev->ifindex, dev->name,
  325. ha->refcount, ha->global_use,
  326. (int)dev->addr_len, ha->addr);
  327. }
  328. netif_addr_unlock_bh(dev);
  329. return 0;
  330. }
  331. static const struct seq_operations dev_mc_seq_ops = {
  332. .start = dev_seq_start,
  333. .next = dev_seq_next,
  334. .stop = dev_seq_stop,
  335. .show = dev_mc_seq_show,
  336. };
  337. static int __net_init dev_mc_net_init(struct net *net)
  338. {
  339. if (!proc_create_net("dev_mcast", 0, net->proc_net, &dev_mc_seq_ops,
  340. sizeof(struct seq_net_private)))
  341. return -ENOMEM;
  342. return 0;
  343. }
  344. static void __net_exit dev_mc_net_exit(struct net *net)
  345. {
  346. remove_proc_entry("dev_mcast", net->proc_net);
  347. }
  348. static struct pernet_operations __net_initdata dev_mc_net_ops = {
  349. .init = dev_mc_net_init,
  350. .exit = dev_mc_net_exit,
  351. };
  352. int __init dev_proc_init(void)
  353. {
  354. int ret = register_pernet_subsys(&dev_proc_ops);
  355. if (!ret)
  356. return register_pernet_subsys(&dev_mc_net_ops);
  357. return ret;
  358. }