xref: /freebsd/sys/contrib/rdma/krping/krping_dev.c (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
1 /*
2  * This code lifted from:
3  * 	Simple `echo' pseudo-device KLD
4  * 	Murray Stokely
5  * 	Converted to 5.X by Søren (Xride) Straarup
6  */
7 
8 /*
9  * /bin/echo "server,port=9999,addr=192.168.69.142,validate" > /dev/krping
10  * /bin/echo "client,port=9999,addr=192.168.69.142,validate" > /dev/krping
11  */
12 
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
15 
16 #include <sys/types.h>
17 #include <sys/module.h>
18 #include <sys/systm.h>  /* uprintf */
19 #include <sys/errno.h>
20 #include <sys/param.h>  /* defines used in kernel.h */
21 #include <sys/kernel.h> /* types used in module initialization */
22 #include <sys/conf.h>   /* cdevsw struct */
23 #include <sys/uio.h>    /* uio struct */
24 #include <sys/malloc.h>
25 #include <sys/proc.h>
26 #include <sys/sysctl.h>
27 #include <machine/stdarg.h>
28 
29 #include "krping.h"
30 
31 #define BUFFERSIZE 512
32 
33 SYSCTL_NODE(_dev, OID_AUTO, krping, CTLFLAG_RW, 0, "kernel rping module");
34 
35 int krping_debug = 0;
36 SYSCTL_INT(_dev_krping, OID_AUTO, debug, CTLFLAG_RW, &krping_debug, 0 , "");
37 
38 /* Function prototypes */
39 static d_open_t      krping_open;
40 static d_close_t     krping_close;
41 static d_read_t      krping_read;
42 static d_write_t     krping_write;
43 
44 /* Character device entry points */
45 static struct cdevsw krping_cdevsw = {
46 	.d_version = D_VERSION,
47 	.d_open = krping_open,
48 	.d_close = krping_close,
49 	.d_read = krping_read,
50 	.d_write = krping_write,
51 	.d_name = "krping",
52 };
53 
54 typedef struct s_krping {
55 	char msg[BUFFERSIZE];
56 	int len;
57 } krping_t;
58 
59 struct stats_list_entry {
60 	STAILQ_ENTRY(stats_list_entry) link;
61 	struct krping_stats *stats;
62 };
63 STAILQ_HEAD(stats_list, stats_list_entry);
64 
65 /* vars */
66 static struct cdev *krping_dev;
67 
68 static int
69 krping_loader(struct module *m, int what, void *arg)
70 {
71 	int err = 0;
72 
73 	switch (what) {
74 	case MOD_LOAD:                /* kldload */
75 		krping_dev = make_dev(&krping_cdevsw, 0, UID_ROOT, GID_WHEEL,
76 					0600, "krping");
77 		printf("Krping device loaded.\n");
78 		break;
79 	case MOD_UNLOAD:
80 		destroy_dev(krping_dev);
81 		printf("Krping device unloaded.\n");
82 		break;
83 	default:
84 		err = EOPNOTSUPP;
85 		break;
86 	}
87 
88 	return (err);
89 }
90 
91 static int
92 krping_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
93 {
94 
95 	return (0);
96 }
97 
98 static int
99 krping_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
100 {
101 
102 	return 0;
103 }
104 
105 static void
106 krping_copy_stats(struct krping_stats *stats, void *arg)
107 {
108 	struct stats_list_entry *s;
109 	struct stats_list *list = arg;
110 
111 	s = malloc(sizeof(*s), M_DEVBUF, M_NOWAIT | M_ZERO);
112 	if (s == NULL)
113 		return;
114 	if (stats != NULL) {
115 		s->stats = malloc(sizeof(*stats), M_DEVBUF, M_NOWAIT | M_ZERO);
116 		if (s->stats == NULL) {
117 			free(s, M_DEVBUF);
118 			return;
119 		}
120 		*s->stats = *stats;
121 	}
122 	STAILQ_INSERT_TAIL(list, s, link);
123 }
124 
125 static int
126 krping_read(struct cdev *dev, struct uio *uio, int ioflag)
127 {
128 	int num = 1;
129 	struct stats_list list;
130 	struct stats_list_entry *e;
131 
132 	STAILQ_INIT(&list);
133 	krping_walk_cb_list(krping_copy_stats, &list);
134 
135 	if (STAILQ_EMPTY(&list))
136 		return (0);
137 
138 	uprintf("krping: %4s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n",
139 	    "num", "device", "snd bytes", "snd msgs", "rcv bytes", "rcv msgs",
140 	    "wr bytes", "wr msgs", "rd bytes", "rd msgs");
141 
142 	while (!STAILQ_EMPTY(&list)) {
143 		e = STAILQ_FIRST(&list);
144 		STAILQ_REMOVE_HEAD(&list, link);
145 		if (e->stats == NULL)
146 			uprintf("krping: %d listen\n", num);
147 		else {
148 			struct krping_stats *stats = e->stats;
149 
150 			uprintf("krping: %4d %10s %10llu %10llu %10llu %10llu "
151 			    "%10llu %10llu %10llu %10llu\n", num, stats->name,
152 			    stats->send_bytes, stats->send_msgs,
153 			    stats->recv_bytes, stats->recv_msgs,
154 			    stats->write_bytes, stats->write_msgs,
155 			    stats->read_bytes, stats->read_msgs);
156 			free(stats, M_DEVBUF);
157 		}
158 		num++;
159 		free(e, M_DEVBUF);
160 	}
161 
162 	return (0);
163 }
164 
165 static int
166 krping_write(struct cdev *dev, struct uio *uio, int ioflag)
167 {
168 	int err = 0;
169 	int amt;
170 	int remain = BUFFERSIZE;
171 	char *cp;
172 	krping_t *krpingmsg;
173 
174 	krpingmsg = malloc(sizeof *krpingmsg, M_DEVBUF, M_WAITOK|M_ZERO);
175 	if (!krpingmsg) {
176 		uprintf("Could not malloc mem!\n");
177 		return ENOMEM;
178 	}
179 
180 	cp = krpingmsg->msg;
181 	while (uio->uio_resid) {
182 		amt = MIN(uio->uio_resid, remain);
183 		if (amt == 0)
184 			break;
185 
186 		/* Copy the string in from user memory to kernel memory */
187 		err = uiomove(cp, amt, uio);
188 		if (err) {
189 			uprintf("Write failed: bad address!\n");
190 			return err;
191 		}
192 		cp += amt;
193 		remain -= amt;
194 	}
195 
196 	if (uio->uio_resid != 0) {
197 		uprintf("Message too big. max size is %d!\n", BUFFERSIZE);
198 		return EMSGSIZE;
199 	}
200 
201 	/* null terminate and remove the \n */
202 	cp--;
203 	*cp = 0;
204 	krpingmsg->len = (unsigned long)(cp - krpingmsg->msg);
205 	uprintf("krping: write string = |%s|\n", krpingmsg->msg);
206 	err = krping_doit(krpingmsg->msg);
207 	free(krpingmsg, M_DEVBUF);
208 	return(err);
209 }
210 
211 int
212 krping_sigpending(void)
213 {
214 
215 	return (SIGPENDING(curthread));
216 }
217 
218 DEV_MODULE(krping, krping_loader, NULL);
219 MODULE_DEPEND(krping, ibcore, 1, 1, 1);
220