summaryrefslogtreecommitdiff
path: root/usr.sbin/switchd/ofrelay.c
blob: 653882caa4ccb9b3b5549ab9b2f1937c1acd4bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*	$OpenBSD: ofrelay.c,v 1.10 2016/12/22 15:31:43 rzalamena Exp $	*/

/*
 * Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/queue.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <event.h>

#include "switchd.h"

void	 ofrelay_close(struct switch_connection *);
void	 ofrelay_event(int, short, void *);
int	 ofrelay_input(int, short, void *);
int	 ofrelay_output(int, short, void *);
void	 ofrelay_accept(int, short, void *);
void	 ofrelay_inflight_dec(struct switch_connection *, const char *);

void	*ofrelay_input_open(struct switch_connection *,
	    struct ibuf *, ssize_t *);
ssize_t	 ofrelay_input_close(struct switch_connection *,
	    struct ibuf *, ssize_t);
int	 ofrelay_input_done(struct switch_connection *, struct ibuf *);
int	 ofrelay_bufget(struct switch_connection *, struct ibuf *);
void	 ofrelay_bufput(struct switch_connection *, struct ibuf *);

volatile int	 ofrelay_sessions;
volatile int	 ofrelay_inflight;
static uint32_t	 ofrelay_conid;

void
ofrelay(struct privsep *ps, struct privsep_proc *p)
{
	struct switchd		*sc = ps->ps_env;
	struct switch_server	*srv = &sc->sc_server;

	log_info("listen on %s", print_host(&srv->srv_addr, NULL, 0));

	if ((srv->srv_fd = switchd_listen((struct sockaddr *)
	    &srv->srv_addr)) == -1)
		fatal("listen");
}

void
ofrelay_run(struct privsep *ps, struct privsep_proc *p, void *arg)
{
	struct switchd		*sc = ps->ps_env;
	struct switch_server	*srv = &sc->sc_server;

	TAILQ_INIT(&sc->sc_conns);
	TAILQ_INIT(&sc->sc_clients);

	srv->srv_sc = sc;
	event_set(&srv->srv_ev, srv->srv_fd, EV_READ, ofrelay_accept, srv);
	event_add(&srv->srv_ev, NULL);
	evtimer_set(&srv->srv_evt, ofrelay_accept, srv);
}

void
ofrelay_close(struct switch_connection *con)
{
	struct switchd		*sc = con->con_sc;
	struct switch_server	*srv = con->con_srv;

	log_info("%s: connection %u.%u closed", __func__,
	    con->con_id, con->con_instance);

	if (event_initialized(&con->con_ev))
		event_del(&con->con_ev);

	TAILQ_REMOVE(&sc->sc_conns, con, con_entry);
	ofrelay_sessions--;

	switch_freetables(con);
	ofp_multipart_clear(con);
	switch_remove(con->con_sc, con->con_switch);
	msgbuf_clear(&con->con_wbuf);
	ibuf_release(con->con_rbuf);
	close(con->con_fd);

	ofrelay_inflight_dec(con, __func__);

	/* Some file descriptors are available again. */
	if (evtimer_pending(&srv->srv_evt, NULL)) {
		DPRINTF("%s: accepting again", __func__);
		evtimer_del(&srv->srv_evt);
		event_add(&srv->srv_ev, NULL);
	}

	free(con);
}

void
ofrelay_event(int fd, short event, void *arg)
{
	struct switch_connection	*con = arg;
	const char			*error = NULL;

	event_add(&con->con_ev, NULL);
	if (event & EV_TIMEOUT) {
		error = "timeout";
		goto fail;
	}
	if (event & EV_WRITE) {
		if (ofrelay_output(fd, event, arg) == -1) {
			error = "write";
			goto fail;
		}
	}
	if (event & EV_READ) {
		if (ofrelay_input(fd, event, arg) == -1) {
			error = "input";
			goto fail;
		}
	}

 fail:
	if (error != NULL) {
		DPRINTF("%s: %s error", __func__, error);
		ofrelay_close(con);
	}
}

int
ofrelay_input(int fd, short event, void *arg)
{
	struct switch_connection	*con = arg;
	struct ibuf		*ibuf = con->con_rbuf;
	ssize_t			 len, rlen;
	size_t			 hlen;
	void			*buf;
	struct ofp_header	*oh = NULL;

	if ((buf = ofrelay_input_open(con, ibuf, &len)) == NULL) {
		log_warn("%s: fd %d, failed to get buffer", __func__, fd);
		return (-1);
	}

	/* If we got a new buffer, read the complete openflow header first */
	if ((hlen = ibuf_length(ibuf)) < sizeof(struct ofp_header)) {
		oh = (struct ofp_header *)ibuf_data(ibuf);
		len = sizeof(*oh) - hlen;
	}

	if ((rlen = read(fd, buf, len)) == -1) {
		if (errno == EINTR || errno == EAGAIN)
			return (0);
		log_warn("%s: fd %d, failed to read", __func__, fd);
		return (-1);
	}

	print_hex(buf, 0, rlen);

	DPRINTF("%s: connection %u.%u read %zd bytes", __func__,
	    con->con_id, con->con_instance, rlen);

	if ((len = ofrelay_input_close(con, ibuf, rlen)) == -1)
		return (-1);
	else if (rlen == 0) {
		/* connection closed */
		ofrelay_input_done(con, ibuf);
		return (-1);
	}

	/* After we verified the openflow header, set the size accordingly */
	if (oh != NULL && (hlen + rlen) == sizeof(*oh)) {
		switch (oh->oh_version) {
		case OFP_V_1_0:
		case OFP_V_1_1:
		case OFP_V_1_2:
		case OFP_V_1_3:
			break;
		default:
			DPRINTF("%s: fd %d, openflow version 0x%02x length %d"
			    " not supported", __func__, fd, oh->oh_version,
			    ntohs(oh->oh_length));
			return (-1);
		}

		len = ntohs(oh->oh_length);
		if (len == sizeof(*oh)) {
			len = 0;
		} else if (len > (ssize_t)ibuf->size) {
			log_debug("%s: buffer too big: %zu > %zd", __func__,
			    len, ibuf->size);
			return (-1);
		} else
			ibuf_setmax(ibuf, len);
	}

	if (len > 0)
		return (len);

	return (ofrelay_input_done(con, ibuf));
}

void
ofrelay_write(struct switch_connection *con, struct ibuf *buf)
{
	ibuf_close(&con->con_wbuf, buf);

	event_del(&con->con_ev);
	event_set(&con->con_ev, con->con_fd, EV_READ|EV_WRITE,
	    ofrelay_event, con);
	event_add(&con->con_ev, NULL);
}

void *
ofrelay_input_open(struct switch_connection *con,
    struct ibuf *buf, ssize_t *len)
{
	ssize_t	 left;

	left = buf->max - buf->wpos;
	if (left == 0) {
		ofrelay_bufget(con, buf);
		left = buf->max;
	}
	if (len)
		*len = left;

	return (buf->buf + buf->wpos);
}

ssize_t
ofrelay_input_close(struct switch_connection *con,
    struct ibuf *buf, ssize_t len)
{
	if (len <= 0)
		return (0);
	if (buf->wpos + len > buf->max)
		return (-1);
	buf->wpos += len;

	return (buf->max - buf->wpos);
}

int
ofrelay_input_done(struct switch_connection *con, struct ibuf *buf)
{
	struct switch_control	*sw;

	if (buf->wpos == 0) {
		ofrelay_bufput(con, buf);
		return (0);
	}

	sw = con->con_switch;
	log_debug("%s: connection %u.%u: %ld bytes from switch %u", __func__,
	    con->con_id, con->con_instance, buf->wpos,
	    sw == NULL ? 0 : sw->sw_id);

	print_hex(buf->buf, 0, buf->wpos);

	if (ofp_input(con, buf) == -1)
		return (-1);

	/* Update read buffer */
	if (ofrelay_bufget(con, buf) == -1)
		return (-1);

	return (0);
}

int
ofrelay_bufget(struct switch_connection *con, struct ibuf *buf)
{
	ibuf_reset(buf);

	/* Should be a static buffer with maximum size */
	if (ibuf_setmax(buf, SWITCHD_MSGBUF_MAX) == -1)
		fatalx("%s: invalid buffer", __func__);

	return (0);
}

void
ofrelay_bufput(struct switch_connection *con, struct ibuf *buf)
{
	/* Just reset the buffer */
	ofrelay_bufget(con, buf);
}

int
ofrelay_output(int fd, short event, void *arg)
{
	struct switch_connection	*con = arg;
	struct msgbuf			*wbuf = &con->con_wbuf;

	if (!wbuf->queued) {
		event_del(&con->con_ev);
		event_set(&con->con_ev, con->con_fd, EV_READ,
		    ofrelay_event, con);
		event_add(&con->con_ev, NULL);
		return (0);
	}

	if (ibuf_write(wbuf) <= 0 && errno != EAGAIN)
		return (-1);

	return (0);
}

void
ofrelay_accept(int fd, short event, void *arg)
{
	struct switch_server	*srv = arg;
	struct sockaddr_storage	 ss;
	int			 s;
	socklen_t		 slen;

	event_add(&srv->srv_ev, NULL);
	if (event & EV_TIMEOUT)
		return;

	slen = sizeof(ss);
	if ((s = accept4_reserve(fd, (struct sockaddr *)&ss, &slen,
	    SOCK_NONBLOCK|SOCK_CLOEXEC, SWITCHD_FD_RESERVE,
	    &ofrelay_inflight)) == -1) {
		/*
		 * Pause accept if we are out of file descriptors, or
		 * libevent will haunt us here too.
		 */
		if (errno == ENFILE || errno == EMFILE) {
			struct timeval evtpause = { 1, 0 };

			event_del(&srv->srv_ev);
			evtimer_add(&srv->srv_evt, &evtpause);
			log_warn("%s: deferring connections", __func__);
		} else
			log_warn("%s accept", __func__);
		return;
	}
	ss.ss_len = slen;

	(void)ofrelay_attach(srv, s, (struct sockaddr *)&ss);
}

void
ofrelay_inflight_dec(struct switch_connection *con, const char *why)
{
	if (con != NULL) {
		/* the flight already left inflight mode. */
		if (con->con_inflight == 0)
			return;
		con->con_inflight = 0;
	}

	/* the file was never opened, thus this was an inflight client. */
	ofrelay_inflight--;
	DPRINTF("%s: inflight decremented, now %d, %s",
	    __func__, ofrelay_inflight, why);
}

int
ofrelay_attach(struct switch_server *srv, int s, struct sockaddr *sa)
{
	struct switchd			*sc = srv->srv_sc;
	struct privsep			*ps = &sc->sc_ps;
	struct switch_connection	*con = NULL;
	socklen_t			 slen;
	int				 ret = -1;

	if (ofrelay_sessions >= SWITCHD_MAX_SESSIONS) {
		log_warn("too many sessions");
		goto done;
	}

	if ((con = calloc(1, sizeof(*con))) == NULL) {
		log_warn("calloc");
		goto done;
	}

	con->con_fd = s;
	con->con_inflight = 1;
	con->con_sc = sc;
	con->con_id = ++ofrelay_conid;
	con->con_instance = ps->ps_instance + 1;
	con->con_srv = srv;
	con->con_state = OFP_STATE_CLOSED;
	SLIST_INIT(&con->con_mmlist);
	TAILQ_INIT(&con->con_stlist);

	memcpy(&con->con_peer, sa, sa->sa_len);
	con->con_port = htons(socket_getport(&con->con_peer));

	if (getsockname(s, (struct sockaddr *)&con->con_local, &slen) == -1) {
		/* Set local sockaddr to AF_UNSPEC */
		memset(&con->con_local, 0, sizeof(con->con_local));
	}

	log_info("%s: new connection %u.%u",
	    __func__, con->con_id, con->con_instance);

	ofrelay_sessions++;
	TAILQ_INSERT_TAIL(&sc->sc_conns, con, con_entry);

	if ((con->con_rbuf = ibuf_static()) == NULL ||
	    ofrelay_bufget(con, con->con_rbuf) == -1) {
		log_warn("ibuf");
		goto done;
	}
	msgbuf_init(&con->con_wbuf);
	con->con_wbuf.fd = s;

	memset(&con->con_ev, 0, sizeof(con->con_ev));
	event_set(&con->con_ev, con->con_fd, EV_READ, ofrelay_event, con);
	event_add(&con->con_ev, NULL);

	ret = ofp_open(ps, con);

 done:
	if (con == NULL)
		close(s);
	ofrelay_inflight_dec(con, __func__);
	if (ret != 0)
		ofrelay_close(con);

	return (ret);
}