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