mpoa_proc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  3. #ifdef CONFIG_PROC_FS
  4. #include <linux/errno.h>
  5. #include <linux/kernel.h>
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/ktime.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/atmmpc.h>
  14. #include <linux/atm.h>
  15. #include <linux/gfp.h>
  16. #include "mpc.h"
  17. #include "mpoa_caches.h"
  18. /*
  19. * mpoa_proc.c: Implementation MPOA client's proc
  20. * file system statistics
  21. */
  22. #if 1
  23. #define dprintk(format, args...) \
  24. printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
  25. #else
  26. #define dprintk(format, args...) \
  27. do { if (0) \
  28. printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
  29. } while (0)
  30. #endif
  31. #if 0
  32. #define ddprintk(format, args...) \
  33. printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
  34. #else
  35. #define ddprintk(format, args...) \
  36. do { if (0) \
  37. printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
  38. } while (0)
  39. #endif
  40. #define STAT_FILE_NAME "mpc" /* Our statistic file's name */
  41. extern struct mpoa_client *mpcs;
  42. extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
  43. static int proc_mpc_open(struct inode *inode, struct file *file);
  44. static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
  45. size_t nbytes, loff_t *ppos);
  46. static int parse_qos(const char *buff);
  47. static const struct proc_ops mpc_proc_ops = {
  48. .proc_open = proc_mpc_open,
  49. .proc_read = seq_read,
  50. .proc_lseek = seq_lseek,
  51. .proc_write = proc_mpc_write,
  52. .proc_release = seq_release,
  53. };
  54. /*
  55. * Returns the state of an ingress cache entry as a string
  56. */
  57. static const char *ingress_state_string(int state)
  58. {
  59. switch (state) {
  60. case INGRESS_RESOLVING:
  61. return "resolving ";
  62. case INGRESS_RESOLVED:
  63. return "resolved ";
  64. case INGRESS_INVALID:
  65. return "invalid ";
  66. case INGRESS_REFRESHING:
  67. return "refreshing ";
  68. }
  69. return "";
  70. }
  71. /*
  72. * Returns the state of an egress cache entry as a string
  73. */
  74. static const char *egress_state_string(int state)
  75. {
  76. switch (state) {
  77. case EGRESS_RESOLVED:
  78. return "resolved ";
  79. case EGRESS_PURGE:
  80. return "purge ";
  81. case EGRESS_INVALID:
  82. return "invalid ";
  83. }
  84. return "";
  85. }
  86. /*
  87. * FIXME: mpcs (and per-mpc lists) have no locking whatsoever.
  88. */
  89. static void *mpc_start(struct seq_file *m, loff_t *pos)
  90. {
  91. loff_t l = *pos;
  92. struct mpoa_client *mpc;
  93. if (!l--)
  94. return SEQ_START_TOKEN;
  95. for (mpc = mpcs; mpc; mpc = mpc->next)
  96. if (!l--)
  97. return mpc;
  98. return NULL;
  99. }
  100. static void *mpc_next(struct seq_file *m, void *v, loff_t *pos)
  101. {
  102. struct mpoa_client *p = v;
  103. (*pos)++;
  104. return v == SEQ_START_TOKEN ? mpcs : p->next;
  105. }
  106. static void mpc_stop(struct seq_file *m, void *v)
  107. {
  108. }
  109. /*
  110. * READING function - called when the /proc/atm/mpoa file is read from.
  111. */
  112. static int mpc_show(struct seq_file *m, void *v)
  113. {
  114. struct mpoa_client *mpc = v;
  115. int i;
  116. in_cache_entry *in_entry;
  117. eg_cache_entry *eg_entry;
  118. time64_t now;
  119. unsigned char ip_string[16];
  120. if (v == SEQ_START_TOKEN) {
  121. atm_mpoa_disp_qos(m);
  122. return 0;
  123. }
  124. seq_printf(m, "\nInterface %d:\n\n", mpc->dev_num);
  125. seq_printf(m, "Ingress Entries:\nIP address State Holding time Packets fwded VPI VCI\n");
  126. now = ktime_get_seconds();
  127. for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
  128. unsigned long seconds_delta = now - in_entry->time;
  129. sprintf(ip_string, "%pI4", &in_entry->ctrl_info.in_dst_ip);
  130. seq_printf(m, "%-16s%s%-14lu%-12u",
  131. ip_string,
  132. ingress_state_string(in_entry->entry_state),
  133. in_entry->ctrl_info.holding_time -
  134. seconds_delta,
  135. in_entry->packets_fwded);
  136. if (in_entry->shortcut)
  137. seq_printf(m, " %-3d %-3d",
  138. in_entry->shortcut->vpi,
  139. in_entry->shortcut->vci);
  140. seq_printf(m, "\n");
  141. }
  142. seq_printf(m, "\n");
  143. seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
  144. for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
  145. unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
  146. unsigned long seconds_delta = now - eg_entry->time;
  147. for (i = 0; i < ATM_ESA_LEN; i++)
  148. seq_printf(m, "%02x", p[i]);
  149. seq_printf(m, "\n%-16lu%s%-14lu%-15u",
  150. (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
  151. egress_state_string(eg_entry->entry_state),
  152. (eg_entry->ctrl_info.holding_time - seconds_delta),
  153. eg_entry->packets_rcvd);
  154. /* latest IP address */
  155. sprintf(ip_string, "%pI4", &eg_entry->latest_ip_addr);
  156. seq_printf(m, "%-16s", ip_string);
  157. if (eg_entry->shortcut)
  158. seq_printf(m, " %-3d %-3d",
  159. eg_entry->shortcut->vpi,
  160. eg_entry->shortcut->vci);
  161. seq_printf(m, "\n");
  162. }
  163. seq_printf(m, "\n");
  164. return 0;
  165. }
  166. static const struct seq_operations mpc_op = {
  167. .start = mpc_start,
  168. .next = mpc_next,
  169. .stop = mpc_stop,
  170. .show = mpc_show
  171. };
  172. static int proc_mpc_open(struct inode *inode, struct file *file)
  173. {
  174. return seq_open(file, &mpc_op);
  175. }
  176. static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
  177. size_t nbytes, loff_t *ppos)
  178. {
  179. char *page, *p;
  180. unsigned int len;
  181. if (nbytes == 0)
  182. return 0;
  183. if (nbytes >= PAGE_SIZE)
  184. nbytes = PAGE_SIZE-1;
  185. page = (char *)__get_free_page(GFP_KERNEL);
  186. if (!page)
  187. return -ENOMEM;
  188. for (p = page, len = 0; len < nbytes; p++, len++) {
  189. if (get_user(*p, buff++)) {
  190. free_page((unsigned long)page);
  191. return -EFAULT;
  192. }
  193. if (*p == '\0' || *p == '\n')
  194. break;
  195. }
  196. *p = '\0';
  197. if (!parse_qos(page))
  198. printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
  199. free_page((unsigned long)page);
  200. return len;
  201. }
  202. static int parse_qos(const char *buff)
  203. {
  204. /* possible lines look like this
  205. * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
  206. */
  207. unsigned char ip[4];
  208. int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
  209. __be32 ipaddr;
  210. struct atm_qos qos;
  211. memset(&qos, 0, sizeof(struct atm_qos));
  212. if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
  213. ip, ip+1, ip+2, ip+3) == 4) {
  214. ipaddr = *(__be32 *)ip;
  215. return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
  216. }
  217. if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=tx",
  218. ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu) == 6) {
  219. rx_pcr = tx_pcr;
  220. rx_sdu = tx_sdu;
  221. } else if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=%d,%d",
  222. ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu, &rx_pcr, &rx_sdu) != 8)
  223. return 0;
  224. ipaddr = *(__be32 *)ip;
  225. qos.txtp.traffic_class = ATM_CBR;
  226. qos.txtp.max_pcr = tx_pcr;
  227. qos.txtp.max_sdu = tx_sdu;
  228. qos.rxtp.traffic_class = ATM_CBR;
  229. qos.rxtp.max_pcr = rx_pcr;
  230. qos.rxtp.max_sdu = rx_sdu;
  231. qos.aal = ATM_AAL5;
  232. dprintk("parse_qos(): setting qos parameters to tx=%d,%d rx=%d,%d\n",
  233. qos.txtp.max_pcr, qos.txtp.max_sdu,
  234. qos.rxtp.max_pcr, qos.rxtp.max_sdu);
  235. atm_mpoa_add_qos(ipaddr, &qos);
  236. return 1;
  237. }
  238. /*
  239. * INITIALIZATION function - called when module is initialized/loaded.
  240. */
  241. int mpc_proc_init(void)
  242. {
  243. struct proc_dir_entry *p;
  244. p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_proc_ops);
  245. if (!p) {
  246. pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
  247. return -ENOMEM;
  248. }
  249. return 0;
  250. }
  251. /*
  252. * DELETING function - called when module is removed.
  253. */
  254. void mpc_proc_clean(void)
  255. {
  256. remove_proc_entry(STAT_FILE_NAME, atm_proc_root);
  257. }
  258. #endif /* CONFIG_PROC_FS */