mpoa_proc.c 7.2 KB

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