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