mpoa_proc.c 6.9 KB

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