Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / vconn-provider.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013 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     const struct vconn_class *class;
34     int state;
35     int error;
36
37     /* OpenFlow versions. */
38     uint32_t allowed_versions;  /* Bitmap of versions we will accept. */
39     uint32_t peer_versions;     /* Peer's bitmap of versions it will accept. */
40     enum ofp_version version;   /* Negotiated version (or 0). */
41     bool recv_any_version;      /* True to receive a message of any version. */
42
43     char *name;
44 };
45
46 void vconn_init(struct vconn *, const struct vconn_class *, int connect_status,
47                 const char *name, uint32_t allowed_versions);
48 void vconn_free_data(struct vconn *vconn);
49 static inline void vconn_assert_class(const struct vconn *vconn,
50                                       const struct vconn_class *class)
51 {
52     ovs_assert(vconn->class == class);
53 }
54
55 struct vconn_class {
56     /* Prefix for connection names, e.g. "nl", "tcp". */
57     const char *name;
58
59     /* Attempts to connect to an OpenFlow device.  'name' is the full
60      * connection name provided by the user, e.g. "tcp:1.2.3.4".  This name is
61      * useful for error messages but must not be modified.
62      *
63      * 'allowed_verions' is the OpenFlow versions that may be
64      * negotiated for a connection.
65      *
66      * 'suffix' is a copy of 'name' following the colon and may be modified.
67      * 'dscp' is the DSCP value that the new connection should use in the IP
68      * packets it sends.
69      *
70      * Returns 0 if successful, otherwise a positive errno value.  If
71      * successful, stores a pointer to the new connection in '*vconnp'.
72      *
73      * The open function must not block waiting for a connection to complete.
74      * If the connection cannot be completed immediately, it should return
75      * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
76      * continue the connection in the background. */
77     int (*open)(const char *name, uint32_t allowed_versions,
78                 char *suffix, struct vconn **vconnp, uint8_t dscp);
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 ofpbuf_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 ofpbuf **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 ofpbuf *msg);
115
116     /* Allows 'vconn' to perform maintenance activities, such as flushing
117      * output buffers.
118      *
119      * May be null if 'vconn' doesn't have anything to do here. */
120     void (*run)(struct vconn *vconn);
121
122     /* Arranges for the poll loop to wake up when 'vconn' needs to perform
123      * maintenance activities.
124      *
125      * May be null if 'vconn' doesn't have anything to do here. */
126     void (*run_wait)(struct vconn *vconn);
127
128     /* Arranges for the poll loop to wake up when 'vconn' is ready to take an
129      * action of the given 'type'. */
130     void (*wait)(struct vconn *vconn, enum vconn_wait_type type);
131 };
132 \f
133 /* Passive virtual connection to an OpenFlow device.
134  *
135  * This structure should be treated as opaque by vconn implementations. */
136 struct pvconn {
137     const struct pvconn_class *class;
138     char *name;
139     uint32_t allowed_versions;
140 };
141
142 void pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class,
143                  const char *name, uint32_t allowed_versions);
144 static inline void pvconn_assert_class(const struct pvconn *pvconn,
145                                        const struct pvconn_class *class)
146 {
147     ovs_assert(pvconn->class == class);
148 }
149
150 struct pvconn_class {
151     /* Prefix for connection names, e.g. "ptcp", "pssl". */
152     const char *name;
153
154     /* Attempts to start listening for OpenFlow connections.  'name' is the
155      * full connection name provided by the user, e.g. "ptcp:1234".  This name
156      * is useful for error messages but must not be modified.
157      *
158      * 'allowed_versions' is the OpenFlow protocol versions that may
159      * be negotiated for a session.
160      *
161      * 'suffix' is a copy of 'name' following the colon and may be modified.
162      * 'dscp' is the DSCP value that the new connection should use in the IP
163      * packets it sends.
164      *
165      * Returns 0 if successful, otherwise a positive errno value.  If
166      * successful, stores a pointer to the new connection in '*pvconnp'.
167      *
168      * The listen function must not block.  If the connection cannot be
169      * completed immediately, it should return EAGAIN (not EINPROGRESS, as
170      * returned by the connect system call) and continue the connection in the
171      * background. */
172     int (*listen)(const char *name, uint32_t allowed_versions,
173                   char *suffix, struct pvconn **pvconnp, uint8_t dscp);
174
175     /* Closes 'pvconn' and frees associated memory. */
176     void (*close)(struct pvconn *pvconn);
177
178     /* Tries to accept a new connection on 'pvconn'.  If successful, stores the
179      * new connection in '*new_vconnp' and returns 0.  Otherwise, returns a
180      * positive errno value.
181      *
182      * The accept function must not block waiting for a connection.  If no
183      * connection is ready to be accepted, it should return EAGAIN. */
184     int (*accept)(struct pvconn *pvconn, struct vconn **new_vconnp);
185
186     /* Arranges for the poll loop to wake up when a connection is ready to be
187      * accepted on 'pvconn'. */
188     void (*wait)(struct pvconn *pvconn);
189 };
190
191 /* Active and passive vconn classes. */
192 extern const struct vconn_class tcp_vconn_class;
193 extern const struct pvconn_class ptcp_pvconn_class;
194 extern const struct vconn_class unix_vconn_class;
195 extern const struct pvconn_class punix_pvconn_class;
196 #ifdef HAVE_OPENSSL
197 extern const struct vconn_class ssl_vconn_class;
198 extern const struct pvconn_class pssl_pvconn_class;
199 #endif
200
201 #endif /* vconn-provider.h */