Add the ability to connect to a vconn asynchronously.
[sliver-openvswitch.git] / include / vconn.h
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #ifndef VCONN_H
23 #define VCONN_H 1
24
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 struct buffer;
29 struct flow;
30 struct pollfd;
31
32 /* Client interface. */
33
34 /* Virtual connection to an OpenFlow device. */
35 struct vconn {
36     struct vconn_class *class;
37     int connect_status;
38 };
39
40 int vconn_open(const char *name, struct vconn **);
41 void vconn_close(struct vconn *);
42 bool vconn_is_passive(const struct vconn *);
43 int vconn_connect(struct vconn *);
44 int vconn_accept(struct vconn *, struct vconn **);
45 int vconn_recv(struct vconn *, struct buffer **);
46 int vconn_send(struct vconn *, struct buffer *);
47
48 int vconn_open_block(const char *name, struct vconn **);
49 int vconn_send_block(struct vconn *, struct buffer *);
50
51 enum vconn_wait_type {
52     WAIT_CONNECT,
53     WAIT_ACCEPT,
54     WAIT_RECV,
55     WAIT_SEND
56 };
57 void vconn_wait(struct vconn *, enum vconn_wait_type);
58 void vconn_connect_wait(struct vconn *);
59 void vconn_accept_wait(struct vconn *);
60 void vconn_recv_wait(struct vconn *);
61 void vconn_send_wait(struct vconn *);
62
63 struct buffer *make_add_simple_flow(const struct flow *,
64                                     uint32_t buffer_id, uint16_t out_port);
65 struct buffer *make_buffered_packet_out(uint32_t buffer_id,
66                                         uint16_t in_port, uint16_t out_port);
67 struct buffer *make_unbuffered_packet_out(const struct buffer *packet,
68                                           uint16_t in_port, uint16_t out_port);
69 \f
70 /* Provider interface. */
71
72 struct vconn_class {
73     /* Prefix for connection names, e.g. "nl", "tcp". */
74     const char *name;
75
76     /* Attempts to connect to an OpenFlow device.  'name' is the full
77      * connection name provided by the user, e.g. "nl:0", "tcp:1.2.3.4".  This
78      * name is useful for error messages but must not be modified.
79      *
80      * 'suffix' is a copy of 'name' following the colon and may be modified.
81      *
82      * Returns 0 if successful, otherwise a positive errno value.  If
83      * successful, stores a pointer to the new connection in '*vconnp'.
84      *
85      * The open function must not block waiting for a connection to complete.
86      * If the connection cannot be completed immediately, it should return
87      * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
88      * continue the connection in the background. */
89     int (*open)(const char *name, char *suffix, struct vconn **vconnp);
90
91     /* Closes 'vconn' and frees associated memory. */
92     void (*close)(struct vconn *vconn);
93
94     /* Tries to complete the connection on 'vconn', which must be an active
95      * vconn.  If 'vconn''s connection is complete, returns 0 if the connection
96      * was successful or a positive errno value if it failed.  If the
97      * connection is still in progress, returns EAGAIN.
98      *
99      * The connect function must not block waiting for the connection to
100      * complete; instead, it should return EAGAIN immediately. */
101     int (*connect)(struct vconn *vconn);
102
103     /* Tries to accept a new connection on 'vconn', which must be a passive
104      * vconn.  If successful, stores the new connection in '*new_vconnp' and
105      * returns 0.  Otherwise, returns a positive errno value.
106      *
107      * The accept function must not block waiting for a connection.  If no
108      * connection is ready to be accepted, it should return EAGAIN.
109      *
110      * Nonnull iff this is a passive vconn (one that accepts connections and
111      * does not transfer data). */
112     int (*accept)(struct vconn *vconn, struct vconn **new_vconnp);
113
114     /* Tries to receive an OpenFlow message from 'vconn', which must be an
115      * active vconn.  If successful, stores the received message into '*msgp'
116      * and returns 0.  The caller is responsible for destroying the message
117      * with buffer_delete().  On failure, returns a positive errno value and
118      * stores a null pointer into '*msgp'.
119      *
120      * If the connection has been closed in the normal fashion, returns EOF.
121      *
122      * The recv function must not block waiting for a packet to arrive.  If no
123      * packets have been received, it should return EAGAIN.
124      *
125      * Nonnull iff this is an active vconn (one that transfers data and does
126      * not accept connections). */
127     int (*recv)(struct vconn *vconn, struct buffer **msgp);
128
129     /* Tries to queue 'msg' for transmission on 'vconn', which must be an
130      * active vconn.  If successful, returns 0, in which case ownership of
131      * 'msg' is transferred to the vconn.  Success does not guarantee that
132      * 'msg' has been or ever will be delivered to the peer, only that it has
133      * been queued for transmission.
134      *
135      * Returns a positive errno value on failure, in which case the caller
136      * retains ownership of 'msg'.
137      *
138      * The send function must not block.  If 'msg' cannot be immediately
139      * accepted for transmission, it should return EAGAIN.
140      *
141      * Nonnull iff this is an active vconn (one that transfers data and does
142      * not accept connections). */
143     int (*send)(struct vconn *vconn, struct buffer *msg);
144
145     void (*wait)(struct vconn *vconn, enum vconn_wait_type);
146 };
147
148 extern struct vconn_class tcp_vconn_class;
149 extern struct vconn_class ptcp_vconn_class;
150 #ifdef HAVE_OPENSSL
151 extern struct vconn_class ssl_vconn_class;
152 extern struct vconn_class pssl_vconn_class;
153 #endif
154 #ifdef HAVE_NETLINK
155 extern struct vconn_class netlink_vconn_class;
156 #endif
157
158 #endif /* vconn.h */