mpoa_proc.c 7.1 KB

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