Make vconn log all incoming and outgoing OpenFlow packets at debug level.
[openvswitch] / lib / vconn.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "vconn.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <netinet/in.h>
38 #include <poll.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "buffer.h"
42 #include "flow.h"
43 #include "ofp-print.h"
44 #include "openflow.h"
45 #include "poll-loop.h"
46 #include "util.h"
47
48 #define THIS_MODULE VLM_vconn
49 #include "vlog.h"
50
51 static struct vconn_class *vconn_classes[] = {
52     &tcp_vconn_class,
53     &ptcp_vconn_class,
54 #ifdef HAVE_NETLINK
55     &netlink_vconn_class,
56 #endif
57 #ifdef HAVE_OPENSSL
58     &ssl_vconn_class,
59     &pssl_vconn_class,
60 #endif
61 };
62
63 /* Check the validity of the vconn class structures. */
64 static void
65 check_vconn_classes(void)
66 {
67 #ifndef NDEBUG
68     size_t i;
69
70     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
71         struct vconn_class *class = vconn_classes[i];
72         assert(class->name != NULL);
73         assert(class->open != NULL);
74         assert(class->close != NULL);
75         assert(class->accept
76                ? !class->recv && !class->send
77                :  class->recv && class->send);
78         assert(class->wait != NULL);
79     }
80 #endif
81 }
82
83 /* Attempts to connect to an OpenFlow device.  'name' is a connection name in
84  * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
85  * vconn class-specific.
86  *
87  * Returns 0 if successful, otherwise a positive errno value.  If successful,
88  * stores a pointer to the new connection in '*vconnp', otherwise a null
89  * pointer.  */
90 int
91 vconn_open(const char *name, struct vconn **vconnp)
92 {
93     size_t prefix_len;
94     size_t i;
95
96     check_vconn_classes();
97
98     prefix_len = strcspn(name, ":");
99     if (prefix_len == strlen(name)) {
100         fatal(0, "`%s' not correct format for peer name", name);
101     }
102     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
103         struct vconn_class *class = vconn_classes[i];
104         if (strlen(class->name) == prefix_len
105             && !memcmp(class->name, name, prefix_len)) {
106             char *suffix_copy = xstrdup(name + prefix_len + 1);
107             int retval = class->open(name, suffix_copy, vconnp);
108             free(suffix_copy);
109             if (retval) {
110                 *vconnp = NULL;
111             } else {
112                 assert((*vconnp)->connect_status != EAGAIN
113                        || (*vconnp)->class->connect);
114             }
115             return retval;
116         }
117     }
118     fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
119     abort();
120 }
121
122 int
123 vconn_open_block(const char *name, struct vconn **vconnp)
124 {
125     struct vconn *vconn;
126     int error;
127
128     error = vconn_open(name, &vconn);
129     while (error == EAGAIN) {
130         vconn_connect_wait(vconn);
131         poll_block();
132         error = vconn_connect(vconn);
133         assert(error != EINPROGRESS);
134     }
135     if (error) {
136         vconn_close(vconn);
137         *vconnp = NULL;
138     } else {
139         *vconnp = vconn;
140     }
141     return error;
142 }
143
144 /* Closes 'vconn'. */
145 void
146 vconn_close(struct vconn *vconn)
147 {
148     if (vconn != NULL) {
149         (vconn->class->close)(vconn);
150     }
151 }
152
153 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
154  * wait for connections to arrive, not to transfer data.  Returns false if
155  * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
156  * to wait for new connections to arrive. */
157 bool
158 vconn_is_passive(const struct vconn *vconn)
159 {
160     return vconn->class->accept != NULL;
161 }
162
163 /* Tries to complete the connection on 'vconn', which must be an active
164  * vconn.  If 'vconn''s connection is complete, returns 0 if the connection
165  * was successful or a positive errno value if it failed.  If the
166  * connection is still in progress, returns EAGAIN. */
167 int
168 vconn_connect(struct vconn *vconn)
169 {
170     if (vconn->connect_status == EAGAIN) {
171         vconn->connect_status = (vconn->class->connect)(vconn);
172         assert(vconn->connect_status != EINPROGRESS);
173     }
174     return vconn->connect_status;
175 }
176
177 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
178  * If successful, stores the new connection in '*new_vconn' and returns 0.
179  * Otherwise, returns a positive errno value.
180  *
181  * vconn_accept will not block waiting for a connection.  If no connection is
182  * ready to be accepted, it returns EAGAIN immediately. */
183 int
184 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
185 {
186     int retval;
187
188     retval = (vconn->class->accept)(vconn, new_vconn);
189
190     if (retval) {
191         *new_vconn = NULL;
192     } else {
193         assert((*new_vconn)->connect_status != EAGAIN
194                || (*new_vconn)->class->connect);
195     }
196     return retval;
197 }
198
199 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
200  * vconn.  If successful, stores the received message into '*msgp' and returns
201  * 0.  The caller is responsible for destroying the message with
202  * buffer_delete().  On failure, returns a positive errno value and stores a
203  * null pointer into '*msgp'.  On normal connection close, returns EOF.
204  *
205  * vconn_recv will not block waiting for a packet to arrive.  If no packets
206  * have been received, it returns EAGAIN immediately. */
207 int
208 vconn_recv(struct vconn *vconn, struct buffer **msgp)
209 {
210     int retval = vconn_connect(vconn);
211     if (!retval) {
212         retval = (vconn->class->recv)(vconn, msgp);
213         if (VLOG_IS_DBG_ENABLED() && !retval) {
214             char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
215             VLOG_DBG("received: %s", s);
216             free(s);
217         }
218     }
219     if (retval) {
220         *msgp = NULL;
221     }
222     return retval;
223 }
224
225 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
226  * vconn.  If successful, returns 0, in which case ownership of 'msg' is
227  * transferred to the vconn.  Success does not guarantee that 'msg' has been or
228  * ever will be delivered to the peer, only that it has been queued for
229  * transmission.
230  *
231  * Returns a positive errno value on failure, in which case the caller
232  * retains ownership of 'msg'.
233  *
234  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
235  * transmission, it returns EAGAIN immediately. */
236 int
237 vconn_send(struct vconn *vconn, struct buffer *msg)
238 {
239     int retval = vconn_connect(vconn);
240     if (!retval) {
241         if (!VLOG_IS_DBG_ENABLED()) { 
242             retval = (vconn->class->send)(vconn, msg);
243         } else {
244             char *s = ofp_to_string(msg->data, msg->size, 1);
245             retval = (vconn->class->send)(vconn, msg);
246             if (retval != EAGAIN) {
247                 VLOG_DBG("sent (%s): %s", strerror(retval), s);
248             }
249             free(s);
250         }
251     }
252     return retval;
253 }
254
255 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
256 int
257 vconn_send_block(struct vconn *vconn, struct buffer *msg)
258 {
259     int retval;
260     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
261         vconn_send_wait(vconn);
262         poll_block();
263     }
264     return retval;
265 }
266
267 void
268 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
269 {
270     int connect_status;
271
272     assert(vconn_is_passive(vconn)
273            ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
274            : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
275
276     connect_status = vconn_connect(vconn);
277     if (connect_status) {
278         if (connect_status == EAGAIN) {
279             wait = WAIT_CONNECT;
280         } else {
281             poll_immediate_wake();
282             return;
283         }
284     }
285
286     (vconn->class->wait)(vconn, wait);
287 }
288
289 void
290 vconn_connect_wait(struct vconn *vconn)
291 {
292     vconn_wait(vconn, WAIT_CONNECT);
293 }
294
295 void
296 vconn_accept_wait(struct vconn *vconn)
297 {
298     vconn_wait(vconn, WAIT_ACCEPT);
299 }
300
301 void
302 vconn_recv_wait(struct vconn *vconn)
303 {
304     vconn_wait(vconn, WAIT_RECV);
305 }
306
307 void
308 vconn_send_wait(struct vconn *vconn)
309 {
310     vconn_wait(vconn, WAIT_SEND);
311 }
312
313 struct buffer *
314 make_add_simple_flow(const struct flow *flow,
315                      uint32_t buffer_id, uint16_t out_port)
316 {
317     struct ofp_flow_mod *ofm;
318     size_t size = sizeof *ofm + sizeof ofm->actions[0];
319     struct buffer *out = buffer_new(size);
320     ofm = buffer_put_uninit(out, size);
321     memset(ofm, 0, size);
322     ofm->header.version = OFP_VERSION;
323     ofm->header.type = OFPT_FLOW_MOD;
324     ofm->header.length = htons(size);
325     ofm->match.wildcards = htons(0);
326     ofm->match.in_port = flow->in_port;
327     memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
328     memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
329     ofm->match.dl_vlan = flow->dl_vlan;
330     ofm->match.dl_type = flow->dl_type;
331     ofm->match.nw_src = flow->nw_src;
332     ofm->match.nw_dst = flow->nw_dst;
333     ofm->match.nw_proto = flow->nw_proto;
334     ofm->match.tp_src = flow->tp_src;
335     ofm->match.tp_dst = flow->tp_dst;
336     ofm->command = htons(OFPFC_ADD);
337     ofm->max_idle = htons(60);
338     ofm->buffer_id = htonl(buffer_id);
339     ofm->group_id = htonl(0);
340     ofm->actions[0].type = htons(OFPAT_OUTPUT);
341     ofm->actions[0].arg.output.max_len = htons(0);
342     ofm->actions[0].arg.output.port = htons(out_port);
343     return out;
344 }
345
346 struct buffer *
347 make_unbuffered_packet_out(const struct buffer *packet,
348                            uint16_t in_port, uint16_t out_port)
349 {
350     struct ofp_packet_out *opo;
351     size_t size = sizeof *opo + packet->size;
352     struct buffer *out = buffer_new(size);
353     opo = buffer_put_uninit(out, size);
354     memset(opo, 0, sizeof *opo);
355     opo->header.version = OFP_VERSION;
356     opo->header.type = OFPT_PACKET_OUT;
357     opo->header.length = htons(size);
358     opo->buffer_id = htonl(UINT32_MAX);
359     opo->in_port = htons(in_port);
360     opo->out_port = htons(out_port);
361     memcpy(opo->u.data, packet->data, packet->size);
362     return out;
363 }
364
365 struct buffer *
366 make_buffered_packet_out(uint32_t buffer_id,
367                          uint16_t in_port, uint16_t out_port)
368 {
369     struct ofp_packet_out *opo;
370     size_t size = sizeof *opo + sizeof opo->u.actions[0];
371     struct buffer *out = buffer_new(size);
372     opo = buffer_put_uninit(out, size);
373     memset(opo, 0, size);
374     opo->header.version = OFP_VERSION;
375     opo->header.type = OFPT_PACKET_OUT;
376     opo->header.length = htons(size);
377     opo->buffer_id = htonl(buffer_id);
378     opo->in_port = htons(in_port);
379     opo->out_port = htons(out_port);
380     opo->u.actions[0].type = htons(OFPAT_OUTPUT);
381     opo->u.actions[0].arg.output.max_len = htons(0);
382     opo->u.actions[0].arg.output.port = htons(out_port);
383     return out;
384 }
385