blob: aae4a87c22ac56e15dc8ffb032917ff659faa1a6 [file] [log] [blame]
Joe Perches99824462010-01-26 11:40:00 +00001#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3#ifdef CONFIG_PROC_FS
4#include <linux/errno.h>
5#include <linux/kernel.h>
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09006#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/mm.h>
8#include <linux/module.h>
9#include <linux/proc_fs.h>
10#include <linux/time.h>
11#include <linux/seq_file.h>
Joe Perchesf1e10042010-01-26 11:40:11 +000012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/atmmpc.h>
14#include <linux/atm.h>
15#include "mpc.h"
16#include "mpoa_caches.h"
17
18/*
19 * mpoa_proc.c: Implementation MPOA client's proc
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090020 * file system statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#if 1
Joe Perchesf1e10042010-01-26 11:40:11 +000024#define dprintk(format, args...) printk(KERN_DEBUG format, ##args) /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#else
Joe Perchesf1e10042010-01-26 11:40:11 +000026#define dprintk(format, args...) \
27 do { if (0) printk(KERN_DEBUG format, ##args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
30#define STAT_FILE_NAME "mpc" /* Our statistic file's name */
31
32extern struct mpoa_client *mpcs;
33extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
34
35static int proc_mpc_open(struct inode *inode, struct file *file);
36static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090037 size_t nbytes, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static int parse_qos(const char *buff);
40
41/*
42 * Define allowed FILE OPERATIONS
43 */
Arjan van de Ven9a321442007-02-12 00:55:35 -080044static const struct file_operations mpc_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .owner = THIS_MODULE,
46 .open = proc_mpc_open,
47 .read = seq_read,
48 .llseek = seq_lseek,
49 .write = proc_mpc_write,
50 .release = seq_release,
51};
52
53/*
54 * Returns the state of an ingress cache entry as a string
55 */
Joe Perchesf1e10042010-01-26 11:40:11 +000056static const char *ingress_state_string(int state)
57{
58 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 case INGRESS_RESOLVING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090060 return "resolving ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 case INGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090062 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 case INGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090064 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 case INGRESS_REFRESHING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090066 return "refreshing ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Joe Perchesf1e10042010-01-26 11:40:11 +000068
69 return "";
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72/*
73 * Returns the state of an egress cache entry as a string
74 */
Joe Perchesf1e10042010-01-26 11:40:11 +000075static const char *egress_state_string(int state)
76{
77 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 case EGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090079 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case EGRESS_PURGE:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090081 return "purge ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 case EGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090083 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Joe Perchesf1e10042010-01-26 11:40:11 +000085
86 return "";
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * FIXME: mpcs (and per-mpc lists) have no locking whatsoever.
91 */
92
93static void *mpc_start(struct seq_file *m, loff_t *pos)
94{
95 loff_t l = *pos;
96 struct mpoa_client *mpc;
97
98 if (!l--)
99 return SEQ_START_TOKEN;
100 for (mpc = mpcs; mpc; mpc = mpc->next)
101 if (!l--)
102 return mpc;
103 return NULL;
104}
105
106static void *mpc_next(struct seq_file *m, void *v, loff_t *pos)
107{
108 struct mpoa_client *p = v;
109 (*pos)++;
110 return v == SEQ_START_TOKEN ? mpcs : p->next;
111}
112
113static void mpc_stop(struct seq_file *m, void *v)
114{
115}
116
117/*
118 * READING function - called when the /proc/atm/mpoa file is read from.
119 */
120static int mpc_show(struct seq_file *m, void *v)
121{
122 struct mpoa_client *mpc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int i;
124 in_cache_entry *in_entry;
125 eg_cache_entry *eg_entry;
126 struct timeval now;
127 unsigned char ip_string[16];
128
129 if (v == SEQ_START_TOKEN) {
130 atm_mpoa_disp_qos(m);
131 return 0;
132 }
133
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900134 seq_printf(m, "\nInterface %d:\n\n", mpc->dev_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 seq_printf(m, "Ingress Entries:\nIP address State Holding time Packets fwded VPI VCI\n");
136 do_gettimeofday(&now);
137
138 for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
Joe Perchesf1e10042010-01-26 11:40:11 +0000139 sprintf(ip_string, "%pI4", &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 seq_printf(m, "%-16s%s%-14lu%-12u",
Joe Perchesf1e10042010-01-26 11:40:11 +0000141 ip_string,
142 ingress_state_string(in_entry->entry_state),
143 in_entry->ctrl_info.holding_time -
144 (now.tv_sec-in_entry->tv.tv_sec),
145 in_entry->packets_fwded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (in_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000147 seq_printf(m, " %-3d %-3d",
148 in_entry->shortcut->vpi,
149 in_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 seq_printf(m, "\n");
151 }
152
153 seq_printf(m, "\n");
154 seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
155 for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
156 unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
Joe Perchesf1e10042010-01-26 11:40:11 +0000157 for (i = 0; i < ATM_ESA_LEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 seq_printf(m, "%02x", p[i]);
159 seq_printf(m, "\n%-16lu%s%-14lu%-15u",
160 (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
161 egress_state_string(eg_entry->entry_state),
Joe Perchesf1e10042010-01-26 11:40:11 +0000162 (eg_entry->ctrl_info.holding_time -
163 (now.tv_sec-eg_entry->tv.tv_sec)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 eg_entry->packets_rcvd);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* latest IP address */
Joe Perchesf1e10042010-01-26 11:40:11 +0000167 sprintf(ip_string, "%pI4", &eg_entry->latest_ip_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 seq_printf(m, "%-16s", ip_string);
169
170 if (eg_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000171 seq_printf(m, " %-3d %-3d",
172 eg_entry->shortcut->vpi,
173 eg_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 seq_printf(m, "\n");
175 }
176 seq_printf(m, "\n");
177 return 0;
178}
179
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700180static const struct seq_operations mpc_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .start = mpc_start,
182 .next = mpc_next,
183 .stop = mpc_stop,
184 .show = mpc_show
185};
186
187static int proc_mpc_open(struct inode *inode, struct file *file)
188{
189 return seq_open(file, &mpc_op);
190}
191
192static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900193 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900195 char *page, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned len;
197
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900198 if (nbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
200
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900201 if (nbytes >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 nbytes = PAGE_SIZE-1;
203
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900204 page = (char *)__get_free_page(GFP_KERNEL);
205 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -ENOMEM;
207
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900208 for (p = page, len = 0; len < nbytes; p++, len++) {
209 if (get_user(*p, buff++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 free_page((unsigned long)page);
211 return -EFAULT;
212 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900213 if (*p == '\0' || *p == '\n')
214 break;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900217 *p = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!parse_qos(page))
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900220 printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900222 free_page((unsigned long)page);
223
224 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static int parse_qos(const char *buff)
228{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900229 /* possible lines look like this
230 * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
231 */
232 unsigned char ip[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900234 __be32 ipaddr;
235 struct atm_qos qos;
236
237 memset(&qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
240 ip, ip+1, ip+2, ip+3) == 4) {
Al Viro30d492d2006-11-14 21:11:29 -0800241 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
243 }
244
245 if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=tx",
246 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu) == 6) {
247 rx_pcr = tx_pcr;
248 rx_sdu = tx_sdu;
249 } else if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=%d,%d",
250 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu, &rx_pcr, &rx_sdu) != 8)
251 return 0;
252
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900253 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 qos.txtp.traffic_class = ATM_CBR;
255 qos.txtp.max_pcr = tx_pcr;
256 qos.txtp.max_sdu = tx_sdu;
257 qos.rxtp.traffic_class = ATM_CBR;
258 qos.rxtp.max_pcr = rx_pcr;
259 qos.rxtp.max_sdu = rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900260 qos.aal = ATM_AAL5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dprintk("mpoa: mpoa_proc.c: parse_qos(): setting qos paramameters to tx=%d,%d rx=%d,%d\n",
262 qos.txtp.max_pcr,
263 qos.txtp.max_sdu,
264 qos.rxtp.max_pcr,
265 qos.rxtp.max_sdu
266 );
267
268 atm_mpoa_add_qos(ipaddr, &qos);
269 return 1;
270}
271
272/*
273 * INITIALIZATION function - called when module is initialized/loaded.
274 */
275int mpc_proc_init(void)
276{
277 struct proc_dir_entry *p;
278
Wang Chen16e297b2008-02-28 13:55:45 -0800279 p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_file_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!p) {
Joe Perches99824462010-01-26 11:40:00 +0000281 pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900282 return -ENOMEM;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285}
286
287/*
288 * DELETING function - called when module is removed.
289 */
290void mpc_proc_clean(void)
291{
Joe Perchesf1e10042010-01-26 11:40:11 +0000292 remove_proc_entry(STAT_FILE_NAME, atm_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#endif /* CONFIG_PROC_FS */
296
297
298
299
300
301