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