Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / vconn-provider.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifndef VCONN_PROVIDER_H
18 #define VCONN_PROVIDER_H 1
19
20 /* Provider interface to vconns, which provide a virtual connection to an
21  * OpenFlow device. */
22
23 #include <assert.h>
24 #include "vconn.h"
25 \f
26 /* Active virtual connection to an OpenFlow device. */
27
28 /* Active virtual connection to an OpenFlow device.
29  *
30  * This structure should be treated as opaque by vconn implementations. */
31 struct vconn {
32     struct vconn_class *class;
33     int state;
34     int error;
35     int min_version;
36     int version;
37     uint32_t ip;
38     char *name;
39     bool reconnectable;
40 };
41
42 void vconn_init(struct vconn *, struct vconn_class *, int connect_status,
43                 uint32_t ip, const char *name, bool reconnectable);
44 static inline void vconn_assert_class(const struct vconn *vconn,
45                                       const struct vconn_class *class)
46 {
47     assert(vconn->class == class);
48 }
49
50 struct vconn_class {
51     /* Prefix for connection names, e.g. "nl", "tcp". */
52     const char *name;
53
54     /* Attempts to connect to an OpenFlow device.  'name' is the full
55      * connection name provided by the user, e.g. "tcp:1.2.3.4".  This name is
56      * useful for error messages but must not be modified.
57      *
58      * 'suffix' is a copy of 'name' following the colon and may be modified.
59      *
60      * Returns 0 if successful, otherwise a positive errno value.  If
61      * successful, stores a pointer to the new connection in '*vconnp'.
62      *
63      * The open function must not block waiting for a connection to complete.
64      * If the connection cannot be completed immediately, it should return
65      * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
66      * continue the connection in the background. */
67     int (*open)(const char *name, char *suffix, struct vconn **vconnp);
68
69     /* Closes 'vconn' and frees associated memory. */
70     void (*close)(struct vconn *vconn);
71
72     /* Tries to complete the connection on 'vconn'.  If 'vconn''s connection is
73      * complete, returns 0 if the connection was successful or a positive errno
74      * value if it failed.  If the connection is still in progress, returns
75      * EAGAIN.
76      *
77      * The connect function must not block waiting for the connection to
78      * complete; instead, it should return EAGAIN immediately. */
79     int (*connect)(struct vconn *vconn);
80
81     /* Tries to receive an OpenFlow message from 'vconn'.  If successful,
82      * stores the received message into '*msgp' and returns 0.  The caller is
83      * responsible for destroying the message with ofpbuf_delete().  On
84      * failure, returns a positive errno value and stores a null pointer into
85      * '*msgp'.
86      *
87      * If the connection has been closed in the normal fashion, returns EOF.
88      *
89      * The recv function must not block waiting for a packet to arrive.  If no
90      * packets have been received, it should return EAGAIN. */
91     int (*recv)(struct vconn *vconn, struct ofpbuf **msgp);
92
93     /* Tries to queue 'msg' for transmission on 'vconn'.  If successful,
94      * returns 0, in which case ownership of 'msg' is transferred to the vconn.
95      * Success does not guarantee that 'msg' has been or ever will be delivered
96      * to the peer, only that it has been queued for transmission.
97      *
98      * Returns a positive errno value on failure, in which case the caller
99      * retains ownership of 'msg'.
100      *
101      * The send function must not block.  If 'msg' cannot be immediately
102      * accepted for transmission, it should return EAGAIN. */
103     int (*send)(struct vconn *vconn, struct ofpbuf *msg);
104
105     /* Arranges for the poll loop to wake up when 'vconn' is ready to take an
106      * action of the given 'type'. */
107     void (*wait)(struct vconn *vconn, enum vconn_wait_type type);
108 };
109 \f
110 /* Passive virtual connection to an OpenFlow device.
111  *
112  * This structure should be treated as opaque by vconn implementations. */
113 struct pvconn {
114     struct pvconn_class *class;
115     char *name;
116 };
117
118 void pvconn_init(struct pvconn *, struct pvconn_class *, const char *name);
119 static inline void pvconn_assert_class(const struct pvconn *pvconn,
120                                        const struct pvconn_class *class)
121 {
122     assert(pvconn->class == class);
123 }
124
125 struct pvconn_class {
126     /* Prefix for connection names, e.g. "ptcp", "pssl". */
127     const char *name;
128
129     /* Attempts to start listening for OpenFlow connections.  'name' is the
130      * full connection name provided by the user, e.g. "ptcp:1234".  This name
131      * is useful for error messages but must not be modified.
132      *
133      * 'suffix' is a copy of 'name' following the colon and may be modified.
134      *
135      * Returns 0 if successful, otherwise a positive errno value.  If
136      * successful, stores a pointer to the new connection in '*pvconnp'.
137      *
138      * The listen function must not block.  If the connection cannot be
139      * completed immediately, it should return EAGAIN (not EINPROGRESS, as
140      * returned by the connect system call) and continue the connection in the
141      * background. */
142     int (*listen)(const char *name, char *suffix, struct pvconn **pvconnp);
143
144     /* Closes 'pvconn' and frees associated memory. */
145     void (*close)(struct pvconn *pvconn);
146
147     /* Tries to accept a new connection on 'pvconn'.  If successful, stores the
148      * new connection in '*new_vconnp' and returns 0.  Otherwise, returns a
149      * positive errno value.
150      *
151      * The accept function must not block waiting for a connection.  If no
152      * connection is ready to be accepted, it should return EAGAIN. */
153     int (*accept)(struct pvconn *pvconn, struct vconn **new_vconnp);
154
155     /* Arranges for the poll loop to wake up when a connection is ready to be
156      * accepted on 'pvconn'. */
157     void (*wait)(struct pvconn *pvconn);
158 };
159
160 /* Active and passive vconn classes. */
161 extern struct vconn_class tcp_vconn_class;
162 extern struct pvconn_class ptcp_pvconn_class;
163 extern struct vconn_class unix_vconn_class;
164 extern struct pvconn_class punix_pvconn_class;
165 #ifdef HAVE_OPENSSL
166 extern struct vconn_class ssl_vconn_class;
167 extern struct pvconn_class pssl_pvconn_class;
168 #endif
169
170 #endif /* vconn-provider.h */