blob: b93cc0f182929e6a52e66bf5eb933bbbd360c19f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Joe Perches99824462010-01-26 11:40:00 +00002#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4#ifdef CONFIG_PROC_FS
5#include <linux/errno.h>
6#include <linux/kernel.h>
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09007#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/module.h>
10#include <linux/proc_fs.h>
Tina Ruchandanid750dbd2017-11-27 15:02:17 +010011#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/seq_file.h>
Joe Perchesf1e10042010-01-26 11:40:11 +000013#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/atmmpc.h>
15#include <linux/atm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "mpc.h"
18#include "mpoa_caches.h"
19
20/*
21 * mpoa_proc.c: Implementation MPOA client's proc
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090022 * file system statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
25#if 1
Joe Perchesb50c2ea2010-01-26 11:40:20 +000026#define dprintk(format, args...) \
27 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000029#define dprintk(format, args...) \
30 do { if (0) \
31 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
32 } while (0)
33#endif
34
35#if 0
36#define ddprintk(format, args...) \
37 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args) /* debug */
38#else
39#define ddprintk(format, args...) \
40 do { if (0) \
41 printk(KERN_DEBUG "mpoa:%s: " format, __FILE__, ##args);\
42 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44
45#define STAT_FILE_NAME "mpc" /* Our statistic file's name */
46
47extern struct mpoa_client *mpcs;
48extern struct proc_dir_entry *atm_proc_root; /* from proc.c. */
49
50static int proc_mpc_open(struct inode *inode, struct file *file);
51static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090052 size_t nbytes, loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static int parse_qos(const char *buff);
55
56/*
57 * Define allowed FILE OPERATIONS
58 */
Arjan van de Ven9a321442007-02-12 00:55:35 -080059static const struct file_operations mpc_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .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;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100140 time64_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 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");
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100150 now = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 for (in_entry = mpc->in_cache; in_entry; in_entry = in_entry->next) {
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100153 unsigned long seconds_delta = now - in_entry->time;
154
Joe Perchesf1e10042010-01-26 11:40:11 +0000155 sprintf(ip_string, "%pI4", &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 seq_printf(m, "%-16s%s%-14lu%-12u",
Joe Perchesf1e10042010-01-26 11:40:11 +0000157 ip_string,
158 ingress_state_string(in_entry->entry_state),
159 in_entry->ctrl_info.holding_time -
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100160 seconds_delta,
Joe Perchesf1e10042010-01-26 11:40:11 +0000161 in_entry->packets_fwded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (in_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000163 seq_printf(m, " %-3d %-3d",
164 in_entry->shortcut->vpi,
165 in_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 seq_printf(m, "\n");
167 }
168
169 seq_printf(m, "\n");
170 seq_printf(m, "Egress Entries:\nIngress MPC ATM addr\nCache-id State Holding time Packets recvd Latest IP addr VPI VCI\n");
171 for (eg_entry = mpc->eg_cache; eg_entry; eg_entry = eg_entry->next) {
172 unsigned char *p = eg_entry->ctrl_info.in_MPC_data_ATM_addr;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100173 unsigned long seconds_delta = now - eg_entry->time;
174
Joe Perchesf1e10042010-01-26 11:40:11 +0000175 for (i = 0; i < ATM_ESA_LEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 seq_printf(m, "%02x", p[i]);
177 seq_printf(m, "\n%-16lu%s%-14lu%-15u",
178 (unsigned long)ntohl(eg_entry->ctrl_info.cache_id),
179 egress_state_string(eg_entry->entry_state),
Tina Ruchandanid750dbd2017-11-27 15:02:17 +0100180 (eg_entry->ctrl_info.holding_time - seconds_delta),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 eg_entry->packets_rcvd);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* latest IP address */
Joe Perchesf1e10042010-01-26 11:40:11 +0000184 sprintf(ip_string, "%pI4", &eg_entry->latest_ip_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 seq_printf(m, "%-16s", ip_string);
186
187 if (eg_entry->shortcut)
Joe Perchesf1e10042010-01-26 11:40:11 +0000188 seq_printf(m, " %-3d %-3d",
189 eg_entry->shortcut->vpi,
190 eg_entry->shortcut->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 seq_printf(m, "\n");
192 }
193 seq_printf(m, "\n");
194 return 0;
195}
196
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700197static const struct seq_operations mpc_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .start = mpc_start,
199 .next = mpc_next,
200 .stop = mpc_stop,
201 .show = mpc_show
202};
203
204static int proc_mpc_open(struct inode *inode, struct file *file)
205{
206 return seq_open(file, &mpc_op);
207}
208
209static ssize_t proc_mpc_write(struct file *file, const char __user *buff,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900210 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900212 char *page, *p;
Eric Dumazet95c96172012-04-15 05:58:06 +0000213 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900215 if (nbytes == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900218 if (nbytes >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 nbytes = PAGE_SIZE-1;
220
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900221 page = (char *)__get_free_page(GFP_KERNEL);
222 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -ENOMEM;
224
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900225 for (p = page, len = 0; len < nbytes; p++, len++) {
226 if (get_user(*p, buff++)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 free_page((unsigned long)page);
228 return -EFAULT;
229 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900230 if (*p == '\0' || *p == '\n')
231 break;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900234 *p = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!parse_qos(page))
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900237 printk("mpoa: proc_mpc_write: could not parse '%s'\n", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900239 free_page((unsigned long)page);
240
241 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static int parse_qos(const char *buff)
245{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900246 /* possible lines look like this
247 * add 130.230.54.142 tx=max_pcr,max_sdu rx=max_pcr,max_sdu
248 */
249 unsigned char ip[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int tx_pcr, tx_sdu, rx_pcr, rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900251 __be32 ipaddr;
252 struct atm_qos qos;
253
254 memset(&qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (sscanf(buff, "del %hhu.%hhu.%hhu.%hhu",
257 ip, ip+1, ip+2, ip+3) == 4) {
Al Viro30d492d2006-11-14 21:11:29 -0800258 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return atm_mpoa_delete_qos(atm_mpoa_search_qos(ipaddr));
260 }
261
262 if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=tx",
263 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu) == 6) {
264 rx_pcr = tx_pcr;
265 rx_sdu = tx_sdu;
266 } else if (sscanf(buff, "add %hhu.%hhu.%hhu.%hhu tx=%d,%d rx=%d,%d",
267 ip, ip+1, ip+2, ip+3, &tx_pcr, &tx_sdu, &rx_pcr, &rx_sdu) != 8)
268 return 0;
269
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900270 ipaddr = *(__be32 *)ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 qos.txtp.traffic_class = ATM_CBR;
272 qos.txtp.max_pcr = tx_pcr;
273 qos.txtp.max_sdu = tx_sdu;
274 qos.rxtp.traffic_class = ATM_CBR;
275 qos.rxtp.max_pcr = rx_pcr;
276 qos.rxtp.max_sdu = rx_sdu;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900277 qos.aal = ATM_AAL5;
Masanari Iidaf42cf8d2015-02-24 23:11:26 +0900278 dprintk("parse_qos(): setting qos parameters to tx=%d,%d rx=%d,%d\n",
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000279 qos.txtp.max_pcr, qos.txtp.max_sdu,
280 qos.rxtp.max_pcr, qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 atm_mpoa_add_qos(ipaddr, &qos);
283 return 1;
284}
285
286/*
287 * INITIALIZATION function - called when module is initialized/loaded.
288 */
289int mpc_proc_init(void)
290{
291 struct proc_dir_entry *p;
292
Wang Chen16e297b2008-02-28 13:55:45 -0800293 p = proc_create(STAT_FILE_NAME, 0, atm_proc_root, &mpc_file_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!p) {
Joe Perches99824462010-01-26 11:40:00 +0000295 pr_err("Unable to initialize /proc/atm/%s\n", STAT_FILE_NAME);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900296 return -ENOMEM;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299}
300
301/*
302 * DELETING function - called when module is removed.
303 */
304void mpc_proc_clean(void)
305{
Joe Perchesf1e10042010-01-26 11:40:11 +0000306 remove_proc_entry(STAT_FILE_NAME, atm_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#endif /* CONFIG_PROC_FS */
310
311
312
313
314
315