mpoa_proc.c 7.3 KB

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