1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
5 #include <linux/errno.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
9 #include <linux/module.h>
10 #include <linux/proc_fs.h>
11 #include <linux/ktime.h>
12 #include <linux/seq_file.h>
13 #include <linux/uaccess.h>
14 #include <linux/atmmpc.h>
15 #include <linux/atm.h>
16 #include <linux/gfp.h>
18 #include "mpoa_caches.h"
21 * mpoa_proc.c: Implementation MPOA client's proc
22 * file system statistics
26 #define dprintk(format, args...) \
27 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
29 #define dprintk(format, args...) \
31 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
36 #define ddprintk(format, args...) \
37 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
39 #define ddprintk(format, args...) \
41 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
45 #define STAT_FILE_NAME "mpc" /* Our statistic file's name */
47 extern struct mpoa_client *mpcs;
48 extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
50 static int proc_mpc_open(struct inode *inode, struct file *file);
51 static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
52 size_t nbytes, loff_t *ppos);
54 static int parse_qos(const char *buff);
56 static const struct proc_ops mpc_proc_ops = {
57 .proc_open = proc_mpc_open,
58 .proc_read = seq_read,
59 .proc_lseek = seq_lseek,
60 .proc_write = proc_mpc_write,
61 .proc_release = seq_release,
65 * Returns the state of an ingress cache entry as a string
67 static const char *ingress_state_string(int state)
70 case INGRESS_RESOLVING:
72 case INGRESS_RESOLVED:
76 case INGRESS_REFRESHING:
84 * Returns the state of an egress cache entry as a string
86 static const char *egress_state_string(int state)
101 * FIXME: mpcs (and per-mpc lists) have no locking whatsoever.
104 static void *mpc_start(struct seq_file *m, loff_t *pos)
107 struct mpoa_client *mpc;
110 return SEQ_START_TOKEN;
111 for (mpc = mpcs; mpc; mpc = mpc->next)
117 static void *mpc_next(struct seq_file *m, void *v, loff_t *pos)
119 struct mpoa_client *p = v;
121 return v == SEQ_START_TOKEN ? mpcs : p->next;
124 static void mpc_stop(struct seq_file *m, void *v)
129 * READING function - called when the /proc/atm/mpoa file is read from.
131 static int mpc_show(struct seq_file *m, void *v)
133 struct mpoa_client *mpc = v;
135 in_cache_entry *in_entry;
136 eg_cache_entry *eg_entry;
138 unsigned char ip_string[16];
140 if (v == SEQ_START_TOKEN) {
141 atm_mpoa_disp_qos(m);
145 seq_printf(m, "\nInterface %d:\n\n", mpc->dev_num);
146 seq_printf(m, "Ingress Entries:\nIP address State Holding time Packets fwded VPI VCI\n");
147 now = ktime_get_seconds();
149 for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
150 unsigned long seconds_delta = now - in_entry->time;
152 sprintf(ip_string, "%pI4", &in_entry->ctrl_info.in_dst_ip);
153 seq_printf(m, "%-16s%s%-14lu%-12u",
155 ingress_state_string(in_entry->entry_state),
156 in_entry->ctrl_info.holding_time -
158 in_entry->packets_fwded);
159 if (in_entry->shortcut)
160 seq_printf(m, " %-3d %-3d",
161 in_entry->shortcut->vpi,
162 in_entry->shortcut->vci);
167 seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
168 for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
169 unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
170 unsigned long seconds_delta = now - eg_entry->time;
172 for (i = 0; i < ATM_ESA_LEN; i++)
173 seq_printf(m, "%02x", p[i]);
174 seq_printf(m, "\n%-16lu%s%-14lu%-15u",
175 (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
176 egress_state_string(eg_entry->entry_state),
177 (eg_entry->ctrl_info.holding_time - seconds_delta),
178 eg_entry->packets_rcvd);
180 /* latest IP address */
181 sprintf(ip_string, "%pI4", &eg_entry->latest_ip_addr);
182 seq_printf(m, "%-16s", ip_string);
184 if (eg_entry->shortcut)
185 seq_printf(m, " %-3d %-3d",
186 eg_entry->shortcut->vpi,
187 eg_entry->shortcut->vci);
194 static const struct seq_operations mpc_op = {
201 static int proc_mpc_open(struct inode *inode, struct file *file)
203 return seq_open(file, &mpc_op);
206 static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
207 size_t nbytes, loff_t *ppos)
215 if (nbytes >= PAGE_SIZE)
216 nbytes = PAGE_SIZE-1;
218 page = (char *)__get_free_page(GFP_KERNEL);
222 for (p = page, len = 0; len < nbytes; p++, len++) {
223 if (get_user(*p, buff++)) {
224 free_page((unsigned long)page);
227 if (*p == '\0' || *p == '\n')
233 if (!parse_qos(page))
234 printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
236 free_page((unsigned long)page);
241 static int parse_qos(const char *buff)
243 /* possible lines look like this
244 * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
247 int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
251 memset(&qos, 0, sizeof(struct atm_qos));
253 if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
254 ip, ip+1, ip+2, ip+3) == 4) {
255 ipaddr = *(__be32 *)ip;
256 return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
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) {
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)
267 ipaddr = *(__be32 *)ip;
268 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;
275 dprintk("parse_qos(): setting qos parameters to tx=%d,%d rx=%d,%d\n",
276 qos.txtp.max_pcr, qos.txtp.max_sdu,
277 qos.rxtp.max_pcr, qos.rxtp.max_sdu);
279 atm_mpoa_add_qos(ipaddr, &qos);
284 * INITIALIZATION function - called when module is initialized/loaded.
286 int mpc_proc_init(void)
288 struct proc_dir_entry *p;
290 p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_proc_ops);
292 pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
299 * DELETING function - called when module is removed.
301 void mpc_proc_clean(void)
303 remove_proc_entry(STAT_FILE_NAME, atm_proc_root);
306 #endif /* CONFIG_PROC_FS */