blob: 0603ab478cf5b203a7a761f4de76373f58ed14b7 [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>
12#include <asm/uaccess.h>
13#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
24#define dprintk printk /* debug */
25#else
26#define dprintk(format,args...)
27#endif
28
29#define STAT_FILE_NAME "mpc" /* Our statistic file's name */
30
31extern struct mpoa_client *mpcs;
32extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
33
34static int proc_mpc_open(struct inode *inode, struct file *file);
35static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090036 size_t nbytes, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static int parse_qos(const char *buff);
39
40/*
41 * Define allowed FILE OPERATIONS
42 */
Arjan van de Ven9a321442007-02-12 00:55:35 -080043static const struct file_operations mpc_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .owner = THIS_MODULE,
45 .open = proc_mpc_open,
46 .read = seq_read,
47 .llseek = seq_lseek,
48 .write = proc_mpc_write,
49 .release = seq_release,
50};
51
52/*
53 * Returns the state of an ingress cache entry as a string
54 */
55static const char *ingress_state_string(int state){
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090056 switch(state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 case INGRESS_RESOLVING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090058 return "resolving ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
60 case INGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090061 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
63 case INGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090064 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
66 case INGRESS_REFRESHING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090067 return "refreshing ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 default:
70 return "";
71 }
72}
73
74/*
75 * Returns the state of an egress cache entry as a string
76 */
77static const char *egress_state_string(int state){
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090078 switch(state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 case EGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090080 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
82 case EGRESS_PURGE:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090083 return "purge ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 case EGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090086 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88 default:
89 return "";
90 }
91}
92
93/*
94 * FIXME: mpcs (and per-mpc lists) have no locking whatsoever.
95 */
96
97static void *mpc_start(struct seq_file *m, loff_t *pos)
98{
99 loff_t l = *pos;
100 struct mpoa_client *mpc;
101
102 if (!l--)
103 return SEQ_START_TOKEN;
104 for (mpc = mpcs; mpc; mpc = mpc->next)
105 if (!l--)
106 return mpc;
107 return NULL;
108}
109
110static void *mpc_next(struct seq_file *m, void *v, loff_t *pos)
111{
112 struct mpoa_client *p = v;
113 (*pos)++;
114 return v == SEQ_START_TOKEN ? mpcs : p->next;
115}
116
117static void mpc_stop(struct seq_file *m, void *v)
118{
119}
120
121/*
122 * READING function - called when the /proc/atm/mpoa file is read from.
123 */
124static int mpc_show(struct seq_file *m, void *v)
125{
126 struct mpoa_client *mpc = v;
127 unsigned char *temp;
128 int i;
129 in_cache_entry *in_entry;
130 eg_cache_entry *eg_entry;
131 struct timeval now;
132 unsigned char ip_string[16];
133
134 if (v == SEQ_START_TOKEN) {
135 atm_mpoa_disp_qos(m);
136 return 0;
137 }
138
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900139 seq_printf(m, "\nInterface %d:\n\n", mpc->dev_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 seq_printf(m, "Ingress Entries:\nIP address State Holding time Packets fwded VPI VCI\n");
141 do_gettimeofday(&now);
142
143 for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
144 temp = (unsigned char *)&in_entry->ctrl_info.in_dst_ip;
145 sprintf(ip_string,"%d.%d.%d.%d", temp[0], temp[1], temp[2], temp[3]);
146 seq_printf(m, "%-16s%s%-14lu%-12u",
147 ip_string,
148 ingress_state_string(in_entry->entry_state),
149 in_entry->ctrl_info.holding_time-(now.tv_sec-in_entry->tv.tv_sec),
150 in_entry->packets_fwded);
151 if (in_entry->shortcut)
152 seq_printf(m, " %-3d %-3d",in_entry->shortcut->vpi,in_entry->shortcut->vci);
153 seq_printf(m, "\n");
154 }
155
156 seq_printf(m, "\n");
157 seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
158 for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
159 unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
160 for(i = 0; i < ATM_ESA_LEN; i++)
161 seq_printf(m, "%02x", p[i]);
162 seq_printf(m, "\n%-16lu%s%-14lu%-15u",
163 (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
164 egress_state_string(eg_entry->entry_state),
165 (eg_entry->ctrl_info.holding_time-(now.tv_sec-eg_entry->tv.tv_sec)),
166 eg_entry->packets_rcvd);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* latest IP address */
169 temp = (unsigned char *)&eg_entry->latest_ip_addr;
170 sprintf(ip_string, "%d.%d.%d.%d", temp[0], temp[1], temp[2], temp[3]);
171 seq_printf(m, "%-16s", ip_string);
172
173 if (eg_entry->shortcut)
174 seq_printf(m, " %-3d %-3d",eg_entry->shortcut->vpi,eg_entry->shortcut->vci);
175 seq_printf(m, "\n");
176 }
177 seq_printf(m, "\n");
178 return 0;
179}
180
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700181static const struct seq_operations mpc_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .start = mpc_start,
183 .next = mpc_next,
184 .stop = mpc_stop,
185 .show = mpc_show
186};
187
188static int proc_mpc_open(struct inode *inode, struct file *file)
189{
190 return seq_open(file, &mpc_op);
191}
192
193static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900194 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900196 char *page, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned len;
198
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900199 if (nbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900202 if (nbytes >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 nbytes = PAGE_SIZE-1;
204
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900205 page = (char *)__get_free_page(GFP_KERNEL);
206 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -ENOMEM;
208
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900209 for (p = page, len = 0; len < nbytes; p++, len++) {
210 if (get_user(*p, buff++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 free_page((unsigned long)page);
212 return -EFAULT;
213 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900214 if (*p == '\0' || *p == '\n')
215 break;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900218 *p = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!parse_qos(page))
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900221 printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900223 free_page((unsigned long)page);
224
225 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static int parse_qos(const char *buff)
229{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900230 /* possible lines look like this
231 * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
232 */
233 unsigned char ip[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900235 __be32 ipaddr;
236 struct atm_qos qos;
237
238 memset(&qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
241 ip, ip+1, ip+2, ip+3) == 4) {
Al Viro30d492d2006-11-14 21:11:29 -0800242 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
244 }
245
246 if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=tx",
247 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu) == 6) {
248 rx_pcr = tx_pcr;
249 rx_sdu = tx_sdu;
250 } else if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=%d,%d",
251 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu, &rx_pcr, &rx_sdu) != 8)
252 return 0;
253
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900254 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 qos.txtp.traffic_class = ATM_CBR;
256 qos.txtp.max_pcr = tx_pcr;
257 qos.txtp.max_sdu = tx_sdu;
258 qos.rxtp.traffic_class = ATM_CBR;
259 qos.rxtp.max_pcr = rx_pcr;
260 qos.rxtp.max_sdu = rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900261 qos.aal = ATM_AAL5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dprintk("mpoa: mpoa_proc.c: parse_qos(): setting qos paramameters to tx=%d,%d rx=%d,%d\n",
263 qos.txtp.max_pcr,
264 qos.txtp.max_sdu,
265 qos.rxtp.max_pcr,
266 qos.rxtp.max_sdu
267 );
268
269 atm_mpoa_add_qos(ipaddr, &qos);
270 return 1;
271}
272
273/*
274 * INITIALIZATION function - called when module is initialized/loaded.
275 */
276int mpc_proc_init(void)
277{
278 struct proc_dir_entry *p;
279
Wang Chen16e297b2008-02-28 13:55:45 -0800280 p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_file_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!p) {
Joe Perches99824462010-01-26 11:40:00 +0000282 pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900283 return -ENOMEM;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286}
287
288/*
289 * DELETING function - called when module is removed.
290 */
291void mpc_proc_clean(void)
292{
293 remove_proc_entry(STAT_FILE_NAME,atm_proc_root);
294}
295
296
297#endif /* CONFIG_PROC_FS */
298
299
300
301
302
303