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