mpoa_proc.c 7.2 KB

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