blob: 2df34eb5d65f4c8a9b190fe7eea6f62bfae0a484 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "mpc.h"
17#include "mpoa_caches.h"
18
19/*
20 * mpoa_proc.c: Implementation MPOA client's proc
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090021 * file system statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
24#if 1
Joe Perchesb50c2ea2010-01-26 11:40:20 +000025#define dprintk(format, args...) \
26 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000028#define dprintk(format, args...) \
29 do { if (0) \
30 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
31 } while (0)
32#endif
33
34#if 0
35#define ddprintk(format, args...) \
36 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
37#else
38#define ddprintk(format, args...) \
39 do { if (0) \
40 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
41 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
43
44#define STAT_FILE_NAME "mpc" /* Our statistic file's name */
45
46extern struct mpoa_client *mpcs;
47extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
48
49static int proc_mpc_open(struct inode *inode, struct file *file);
50static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090051 size_t nbytes, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static int parse_qos(const char *buff);
54
55/*
56 * Define allowed FILE OPERATIONS
57 */
Arjan van de Ven9a321442007-02-12 00:55:35 -080058static const struct file_operations mpc_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .owner = THIS_MODULE,
60 .open = proc_mpc_open,
61 .read = seq_read,
62 .llseek = seq_lseek,
63 .write = proc_mpc_write,
64 .release = seq_release,
65};
66
67/*
68 * Returns the state of an ingress cache entry as a string
69 */
Joe Perchesf1e10042010-01-26 11:40:11 +000070static const char *ingress_state_string(int state)
71{
72 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 case INGRESS_RESOLVING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090074 return "resolving ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 case INGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090076 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 case INGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090078 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 case INGRESS_REFRESHING:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090080 return "refreshing ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Joe Perchesf1e10042010-01-26 11:40:11 +000082
83 return "";
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86/*
87 * Returns the state of an egress cache entry as a string
88 */
Joe Perchesf1e10042010-01-26 11:40:11 +000089static const char *egress_state_string(int state)
90{
91 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case EGRESS_RESOLVED:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090093 return "resolved ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 case EGRESS_PURGE:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090095 return "purge ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 case EGRESS_INVALID:
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090097 return "invalid ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Joe Perchesf1e10042010-01-26 11:40:11 +000099
100 return "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
104 * FIXME: mpcs (and per-mpc lists) have no locking whatsoever.
105 */
106
107static void *mpc_start(struct seq_file *m, loff_t *pos)
108{
109 loff_t l = *pos;
110 struct mpoa_client *mpc;
111
112 if (!l--)
113 return SEQ_START_TOKEN;
114 for (mpc = mpcs; mpc; mpc = mpc->next)
115 if (!l--)
116 return mpc;
117 return NULL;
118}
119
120static void *mpc_next(struct seq_file *m, void *v, loff_t *pos)
121{
122 struct mpoa_client *p = v;
123 (*pos)++;
124 return v == SEQ_START_TOKEN ? mpcs : p->next;
125}
126
127static void mpc_stop(struct seq_file *m, void *v)
128{
129}
130
131/*
132 * READING function - called when the /proc/atm/mpoa file is read from.
133 */
134static int mpc_show(struct seq_file *m, void *v)
135{
136 struct mpoa_client *mpc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int i;
138 in_cache_entry *in_entry;
139 eg_cache_entry *eg_entry;
140 struct timeval now;
141 unsigned char ip_string[16];
142
143 if (v == SEQ_START_TOKEN) {
144 atm_mpoa_disp_qos(m);
145 return 0;
146 }
147
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900148 seq_printf(m, "\nInterface %d:\n\n", mpc->dev_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 seq_printf(m, "Ingress Entries:\nIP address State Holding time Packets fwded VPI VCI\n");
150 do_gettimeofday(&now);
151
152 for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
Joe Perchesf1e10042010-01-26 11:40:11 +0000153 sprintf(ip_string, "%pI4", &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 seq_printf(m, "%-16s%s%-14lu%-12u",
Joe Perchesf1e10042010-01-26 11:40:11 +0000155 ip_string,
156 ingress_state_string(in_entry->entry_state),
157 in_entry->ctrl_info.holding_time -
158 (now.tv_sec-in_entry->tv.tv_sec),
159 in_entry->packets_fwded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (in_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000161 seq_printf(m, " %-3d %-3d",
162 in_entry->shortcut->vpi,
163 in_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 seq_printf(m, "\n");
165 }
166
167 seq_printf(m, "\n");
168 seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
169 for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
170 unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
Joe Perchesf1e10042010-01-26 11:40:11 +0000171 for (i = 0; i < ATM_ESA_LEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 seq_printf(m, "%02x", p[i]);
173 seq_printf(m, "\n%-16lu%s%-14lu%-15u",
174 (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
175 egress_state_string(eg_entry->entry_state),
Joe Perchesf1e10042010-01-26 11:40:11 +0000176 (eg_entry->ctrl_info.holding_time -
177 (now.tv_sec-eg_entry->tv.tv_sec)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 eg_entry->packets_rcvd);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* latest IP address */
Joe Perchesf1e10042010-01-26 11:40:11 +0000181 sprintf(ip_string, "%pI4", &eg_entry->latest_ip_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 seq_printf(m, "%-16s", ip_string);
183
184 if (eg_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000185 seq_printf(m, " %-3d %-3d",
186 eg_entry->shortcut->vpi,
187 eg_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 seq_printf(m, "\n");
189 }
190 seq_printf(m, "\n");
191 return 0;
192}
193
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700194static const struct seq_operations mpc_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .start = mpc_start,
196 .next = mpc_next,
197 .stop = mpc_stop,
198 .show = mpc_show
199};
200
201static int proc_mpc_open(struct inode *inode, struct file *file)
202{
203 return seq_open(file, &mpc_op);
204}
205
206static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900207 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900209 char *page, *p;
Eric Dumazet95c96172012-04-15 05:58:06 +0000210 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900212 if (nbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900215 if (nbytes >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 nbytes = PAGE_SIZE-1;
217
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900218 page = (char *)__get_free_page(GFP_KERNEL);
219 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENOMEM;
221
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900222 for (p = page, len = 0; len < nbytes; p++, len++) {
223 if (get_user(*p, buff++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 free_page((unsigned long)page);
225 return -EFAULT;
226 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900227 if (*p == '\0' || *p == '\n')
228 break;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900231 *p = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!parse_qos(page))
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900234 printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900236 free_page((unsigned long)page);
237
238 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static int parse_qos(const char *buff)
242{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900243 /* possible lines look like this
244 * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
245 */
246 unsigned char ip[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900248 __be32 ipaddr;
249 struct atm_qos qos;
250
251 memset(&qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
254 ip, ip+1, ip+2, ip+3) == 4) {
Al Viro30d492d2006-11-14 21:11:29 -0800255 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
257 }
258
259 if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=tx",
260 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu) == 6) {
261 rx_pcr = tx_pcr;
262 rx_sdu = tx_sdu;
263 } else if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=%d,%d",
264 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu, &rx_pcr, &rx_sdu) != 8)
265 return 0;
266
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900267 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 qos.txtp.traffic_class = ATM_CBR;
269 qos.txtp.max_pcr = tx_pcr;
270 qos.txtp.max_sdu = tx_sdu;
271 qos.rxtp.traffic_class = ATM_CBR;
272 qos.rxtp.max_pcr = rx_pcr;
273 qos.rxtp.max_sdu = rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900274 qos.aal = ATM_AAL5;
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900275 dprintk("parse_qos(): setting qos parameters to tx=%d,%d rx=%d,%d\n",
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000276 qos.txtp.max_pcr, qos.txtp.max_sdu,
277 qos.rxtp.max_pcr, qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 atm_mpoa_add_qos(ipaddr, &qos);
280 return 1;
281}
282
283/*
284 * INITIALIZATION function - called when module is initialized/loaded.
285 */
286int mpc_proc_init(void)
287{
288 struct proc_dir_entry *p;
289
Wang Chen16e297b2008-02-28 13:55:45 -0800290 p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_file_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!p) {
Joe Perches99824462010-01-26 11:40:00 +0000292 pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900293 return -ENOMEM;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
298/*
299 * DELETING function - called when module is removed.
300 */
301void mpc_proc_clean(void)
302{
Joe Perchesf1e10042010-01-26 11:40:11 +0000303 remove_proc_entry(STAT_FILE_NAME, atm_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif /* CONFIG_PROC_FS */
307
308
309
310
311
312