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