mpoa_proc.c 6.8 KB

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