Replace most uses of assert by ovs_assert.
[sliver-openvswitch.git] / lib / vconn-provider.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 "vconn.h"
24 #include "util.h"
25 #include "openflow/openflow-common.h"
26 \f
27 /* Active virtual connection to an OpenFlow device. */
28
29 /* Active virtual connection to an OpenFlow device.
30  *
31  * This structure should be treated as opaque by vconn implementations. */
32 struct vconn {
33     struct vconn_class *class;
34     int state;
35     int error;
36     uint32_t allowed_versions;
37     uint32_t peer_versions;
38     enum ofp_version version;
39     ovs_be32 remote_ip;
40     ovs_be16 remote_port;
41     ovs_be32 local_ip;
42     ovs_be16 local_port;
43     char *name;
44 };
45
46 void vconn_init(struct vconn *, struct vconn_class *, int connect_status,
47                 const char *name, uint32_t allowed_versions);
48 void vconn_free_data(struct vconn *vconn);
49 void vconn_set_remote_ip(struct vconn *, ovs_be32 remote_ip);
50 void vconn_set_remote_port(struct vconn *, ovs_be16 remote_port);
51 void vconn_set_local_ip(struct vconn *, ovs_be32 local_ip);
52 void vconn_set_local_port(struct vconn *, ovs_be16 local_port);
53 static inline void vconn_assert_class(const struct vconn *vconn,
54                                       const struct vconn_class *class)
55 {
56     ovs_assert(vconn->class == class);
57 }
58
59 struct vconn_class {
60     /* Prefix for connection names, e.g. "nl", "tcp". */
61     const char *name;
62
63     /* Attempts to connect to an OpenFlow device.  'name' is the full
64      * connection name provided by the user, e.g. "tcp:1.2.3.4".  This name is
65      * useful for error messages but must not be modified.
66      *
67      * 'allowed_verions' is the OpenFlow versions that may be
68      * negotiated for a connection.
69      *
70      * 'suffix' is a copy of 'name' following the colon and may be modified.
71      * 'dscp' is the DSCP value that the new connection should use in the IP
72      * packets it sends.
73      *
74      * Returns 0 if successful, otherwise a positive errno value.  If
75      * successful, stores a pointer to the new connection in '*vconnp'.
76      *
77      * The open function must not block waiting for a connection to complete.
78      * If the connection cannot be completed immediately, it should return
79      * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
80      * continue the connection in the background. */
81     int (*open)(const char *name, uint32_t allowed_versions,
82                 char *suffix, struct vconn **vconnp, uint8_t dscp);
83
84     /* Closes 'vconn' and frees associated memory. */
85     void (*close)(struct vconn *vconn);
86
87     /* Tries to complete the connection on 'vconn'.  If 'vconn''s connection is
88      * complete, returns 0 if the connection was successful or a positive errno
89      * value if it failed.  If the connection is still in progress, returns
90      * EAGAIN.
91      *
92      * The connect function must not block waiting for the connection to
93      * complete; instead, it should return EAGAIN immediately. */
94     int (*connect)(struct vconn *vconn);
95
96     /* Tries to receive an OpenFlow message from 'vconn'.  If successful,
97      * stores the received message into '*msgp' and returns 0.  The caller is
98      * responsible for destroying the message with ofpbuf_delete().  On
99      * failure, returns a positive errno value and stores a null pointer into
100      * '*msgp'.
101      *
102      * If the connection has been closed in the normal fashion, returns EOF.
103      *
104      * The recv function must not block waiting for a packet to arrive.  If no
105      * packets have been received, it should return EAGAIN. */
106     int (*recv)(struct vconn *vconn, struct ofpbuf **msgp);
107
108     /* Tries to queue 'msg' for transmission on 'vconn'.  If successful,
109      * returns 0, in which case ownership of 'msg' is transferred to the vconn.
110      * Success does not guarantee that 'msg' has been or ever will be delivered
111      * to the peer, only that it has been queued for transmission.
112      *
113      * Returns a positive errno value on failure, in which case the caller
114      * retains ownership of 'msg'.
115      *
116      * The send function must not block.  If 'msg' cannot be immediately
117      * accepted for transmission, it should return EAGAIN. */
118     int (*send)(struct vconn *vconn, struct ofpbuf *msg);
119
120     /* Allows 'vconn' to perform maintenance activities, such as flushing
121      * output buffers.
122      *
123      * May be null if 'vconn' doesn't have anything to do here. */
124     void (*run)(struct vconn *vconn);
125
126     /* Arranges for the poll loop to wake up when 'vconn' needs to perform
127      * maintenance activities.
128      *
129      * May be null if 'vconn' doesn't have anything to do here. */
130     void (*run_wait)(struct vconn *vconn);
131
132     /* Arranges for the poll loop to wake up when 'vconn' is ready to take an
133      * action of the given 'type'. */
134     void (*wait)(struct vconn *vconn, enum vconn_wait_type type);
135 };
136 \f
137 /* Passive virtual connection to an OpenFlow device.
138  *
139  * This structure should be treated as opaque by vconn implementations. */
140 struct pvconn {
141     struct pvconn_class *class;
142     char *name;
143     uint32_t allowed_versions;
144 };
145
146 void pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
147                  const char *name, uint32_t allowed_versions);
148 static inline void pvconn_assert_class(const struct pvconn *pvconn,
149                                        const struct pvconn_class *class)
150 {
151     ovs_assert(pvconn->class == class);
152 }
153
154 struct pvconn_class {
155     /* Prefix for connection names, e.g. "ptcp", "pssl". */
156     const char *name;
157
158     /* Attempts to start listening for OpenFlow connections.  'name' is the
159      * full connection name provided by the user, e.g. "ptcp:1234".  This name
160      * is useful for error messages but must not be modified.
161      *
162      * 'allowed_versions' is the OpenFlow protocol versions that may
163      * be negotiated for a session.
164      *
165      * 'suffix' is a copy of 'name' following the colon and may be modified.
166      * 'dscp' is the DSCP value that the new connection should use in the IP
167      * packets it sends.
168      *
169      * Returns 0 if successful, otherwise a positive errno value.  If
170      * successful, stores a pointer to the new connection in '*pvconnp'.
171      *
172      * The listen function must not block.  If the connection cannot be
173      * completed immediately, it should return EAGAIN (not EINPROGRESS, as
174      * returned by the connect system call) and continue the connection in the
175      * background. */
176     int (*listen)(const char *name, uint32_t allowed_versions,
177                   char *suffix, struct pvconn **pvconnp, uint8_t dscp);
178
179     /* Closes 'pvconn' and frees associated memory. */
180     void (*close)(struct pvconn *pvconn);
181
182     /* Tries to accept a new connection on 'pvconn'.  If successful, stores the
183      * new connection in '*new_vconnp' and returns 0.  Otherwise, returns a
184      * positive errno value.
185      *
186      * The accept function must not block waiting for a connection.  If no
187      * connection is ready to be accepted, it should return EAGAIN. */
188     int (*accept)(struct pvconn *pvconn, struct vconn **new_vconnp);
189
190     /* Arranges for the poll loop to wake up when a connection is ready to be
191      * accepted on 'pvconn'. */
192     void (*wait)(struct pvconn *pvconn);
193 };
194
195 /* Active and passive vconn classes. */
196 extern struct vconn_class tcp_vconn_class;
197 extern struct pvconn_class ptcp_pvconn_class;
198 extern struct vconn_class unix_vconn_class;
199 extern struct pvconn_class punix_pvconn_class;
200 #ifdef HAVE_OPENSSL
201 extern struct vconn_class ssl_vconn_class;
202 extern struct pvconn_class pssl_pvconn_class;
203 #endif
204
205 #endif /* vconn-provider.h */