Break passive vconns out into separate pvconn routines and data structures.
[openvswitch] / include / vconn-provider.h
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 #ifndef VCONN_PROVIDER_H
35 #define VCONN_PROVIDER_H 1
36
37 /* Provider interface to vconns, which provide a virtual connection to an
38  * OpenFlow device. */
39
40 #include <assert.h>
41 #include "vconn.h"
42 \f
43 /* Active virtual connection to an OpenFlow device.
44  *
45  * This structure should be treated as opaque by vconn implementations. */
46 struct vconn {
47     struct vconn_class *class;
48     int connect_status;
49     uint32_t ip;
50     char *name;
51 };
52
53 void vconn_init(struct vconn *, struct vconn_class *, int connect_status,
54                 uint32_t ip, const char *name);
55 static inline void vconn_assert_class(const struct vconn *vconn,
56                                       const struct vconn_class *class)
57 {
58     assert(vconn->class == class);
59 }
60
61 struct vconn_class {
62     /* Prefix for connection names, e.g. "nl", "tcp". */
63     const char *name;
64
65     /* Attempts to connect to an OpenFlow device.  'name' is the full
66      * connection name provided by the user, e.g. "nl:0", "tcp:1.2.3.4".  This
67      * name is useful for error messages but must not be modified.
68      *
69      * 'suffix' is a copy of 'name' following the colon and may be modified.
70      *
71      * Returns 0 if successful, otherwise a positive errno value.  If
72      * successful, stores a pointer to the new connection in '*vconnp'.
73      *
74      * The open function must not block waiting for a connection to complete.
75      * If the connection cannot be completed immediately, it should return
76      * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
77      * continue the connection in the background. */
78     int (*open)(const char *name, char *suffix, struct vconn **vconnp);
79
80     /* Closes 'vconn' and frees associated memory. */
81     void (*close)(struct vconn *vconn);
82
83     /* Tries to complete the connection on 'vconn'.  If 'vconn''s connection is
84      * complete, returns 0 if the connection was successful or a positive errno
85      * value if it failed.  If the connection is still in progress, returns
86      * EAGAIN.
87      *
88      * The connect function must not block waiting for the connection to
89      * complete; instead, it should return EAGAIN immediately. */
90     int (*connect)(struct vconn *vconn);
91
92     /* Tries to receive an OpenFlow message from 'vconn'.  If successful,
93      * stores the received message into '*msgp' and returns 0.  The caller is
94      * responsible for destroying the message with buffer_delete().  On
95      * failure, returns a positive errno value and stores a null pointer into
96      * '*msgp'.
97      *
98      * If the connection has been closed in the normal fashion, returns EOF.
99      *
100      * The recv function must not block waiting for a packet to arrive.  If no
101      * packets have been received, it should return EAGAIN. */
102     int (*recv)(struct vconn *vconn, struct buffer **msgp);
103
104     /* Tries to queue 'msg' for transmission on 'vconn'.  If successful,
105      * returns 0, in which case ownership of 'msg' is transferred to the vconn.
106      * Success does not guarantee that 'msg' has been or ever will be delivered
107      * to the peer, only that it has been queued for transmission.
108      *
109      * Returns a positive errno value on failure, in which case the caller
110      * retains ownership of 'msg'.
111      *
112      * The send function must not block.  If 'msg' cannot be immediately
113      * accepted for transmission, it should return EAGAIN. */
114     int (*send)(struct vconn *vconn, struct buffer *msg);
115
116     /* Arranges for the poll loop to wake up when 'vconn' is ready to take an
117      * action of the given 'type'. */
118     void (*wait)(struct vconn *vconn, enum vconn_wait_type type);
119 };
120 \f
121 /* Passive virtual connection to an OpenFlow device.
122  *
123  * This structure should be treated as opaque by vconn implementations. */
124 struct pvconn {
125     struct pvconn_class *class;
126     char *name;
127 };
128
129 void pvconn_init(struct pvconn *, struct pvconn_class *, const char *name);
130 static inline void pvconn_assert_class(const struct pvconn *pvconn,
131                                        const struct pvconn_class *class)
132 {
133     assert(pvconn->class == class);
134 }
135
136 struct pvconn_class {
137     /* Prefix for connection names, e.g. "ptcp", "pssl". */
138     const char *name;
139
140     /* Attempts to start listening for OpenFlow connections.  'name' is the
141      * full connection name provided by the user, e.g. "nl:0", "tcp:1.2.3.4".
142      * This name is useful for error messages but must not be modified.
143      *
144      * 'suffix' is a copy of 'name' following the colon and may be modified.
145      *
146      * Returns 0 if successful, otherwise a positive errno value.  If
147      * successful, stores a pointer to the new connection in '*pvconnp'.
148      *
149      * The listen function must not block.  If the connection cannot be
150      * completed immediately, it should return EAGAIN (not EINPROGRESS, as
151      * returned by the connect system call) and continue the connection in the
152      * background. */
153     int (*listen)(const char *name, char *suffix, struct pvconn **pvconnp);
154
155     /* Closes 'pvconn' and frees associated memory. */
156     void (*close)(struct pvconn *pvconn);
157
158     /* Tries to accept a new connection on 'pvconn'.  If successful, stores the
159      * new connection in '*new_vconnp' and returns 0.  Otherwise, returns a
160      * positive errno value.
161      *
162      * The accept function must not block waiting for a connection.  If no
163      * connection is ready to be accepted, it should return EAGAIN. */
164     int (*accept)(struct pvconn *pvconn, struct vconn **new_vconnp);
165
166     /* Arranges for the poll loop to wake up when a connection is ready to be
167      * accepted on 'pvconn'. */
168     void (*wait)(struct pvconn *pvconn);
169 };
170
171 /* Active and passive vconn classes. */
172 extern struct vconn_class tcp_vconn_class;
173 extern struct pvconn_class ptcp_pvconn_class;
174 extern struct vconn_class unix_vconn_class;
175 extern struct pvconn_class punix_pvconn_class;
176 #ifdef HAVE_OPENSSL
177 extern struct vconn_class ssl_vconn_class;
178 extern struct pvconn_class pssl_pvconn_class;
179 #endif
180 #ifdef HAVE_NETLINK
181 extern struct vconn_class netlink_vconn_class;
182 #endif
183
184 #endif /* vconn-provider.h */