Support SSL in secchan and controller.
[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 };
38
39 /* What kind of operation do we want to perform? */
40 enum {
41     WANT_ACCEPT = 1 << 0,          /* Want to accept a new connection. */
42     WANT_RECV = 1 << 1,            /* Want to receive a message. */
43     WANT_SEND = 1 << 2             /* Want to send a message. */
44 };
45
46 int vconn_open(const char *name, struct vconn **);
47 void vconn_close(struct vconn *);
48 bool vconn_is_passive(const struct vconn *);
49 bool vconn_prepoll(struct vconn *, int want, struct pollfd *);
50 void vconn_postpoll(struct vconn *, short int *revents);
51 int vconn_accept(struct vconn *, struct vconn **);
52 int vconn_recv(struct vconn *, struct buffer **);
53 int vconn_send(struct vconn *, struct buffer *);
54 int vconn_send_wait(struct vconn *, struct buffer *);
55
56 struct buffer *make_add_simple_flow(const struct flow *,
57                                     uint32_t buffer_id, uint16_t out_port);
58 struct buffer *make_buffered_packet_out(uint32_t buffer_id,
59                                         uint16_t in_port, uint16_t out_port);
60 struct buffer *make_unbuffered_packet_out(const struct buffer *packet,
61                                           uint16_t in_port, uint16_t out_port);
62 \f
63 /* Provider interface. */
64
65 struct vconn_class {
66     /* Prefix for connection names, e.g. "nl", "tcp". */
67     const char *name;
68
69     /* Attempts to connect to an OpenFlow device.  'name' is the full
70      * connection name provided by the user, e.g. "nl:0", "tcp:1.2.3.4".  This
71      * name is useful for error messages but must not be modified.
72      * 
73      * 'suffix' is a copy of 'name' following the colon and may be modified.
74      *
75      * Returns 0 if successful, otherwise a positive errno value.  If
76      * successful, stores a pointer to the new connection in '*vconnp'. */
77     int (*open)(const char *name, char *suffix, struct vconn **vconnp);
78
79     /* Closes 'vconn' and frees associated memory. */
80     void (*close)(struct vconn *vconn);
81
82     /* Called by the main loop before calling poll(), this function must
83      * initialize 'pfd->fd' and 'pfd->events' appropriately so that poll() will
84      * wake up when the connection becomes available for the operations
85      * specified in 'want'.  The prepoll function may also set bits in 'pfd' to
86      * allow for internal processing.
87      *
88      * Should return false normally.  May return true to indicate that no
89      * blocking should happen in poll() because the connection is available for
90      * some operation specified in 'want' but that status cannot be detected
91      * via poll() and thus poll() could block forever otherwise. */
92     bool (*prepoll)(struct vconn *, int want, struct pollfd *pfd);
93
94     /* Called by the main loop after calling poll(), this function may perform
95      * any internal processing needed by the connection.  It is provided with
96      * the vconn file descriptor's status in '*revents', as reported by poll().
97      *
98      * The postpoll function should adjust '*revents' to reflect the status of
99      * the connection from the caller's point of view: that is, upon return
100      * '*revents & POLLIN' should indicate that a packet is (potentially) ready
101      * to be read (for an active vconn) or a new connection is ready to be
102      * accepted (for a passive vconn) and '*revents & POLLOUT' should indicate
103      * that a packet is (potentially) ready to be written.
104      *
105      * This function may be a null pointer in a vconn class that has no use for
106      * it, that is, if the vconn does not need to do any internal processing
107      * and poll's revents out properly reflects the vconn's status.  */
108     void (*postpoll)(struct vconn *, short int *revents);
109
110     /* Tries to accept a new connection on 'vconn', which must be a passive
111      * vconn.  If successful, stores the new connection in '*new_vconnp' and
112      * returns 0.  Otherwise, returns a positive errno value.
113      *
114      * The accept function must not block waiting for a connection.  If no
115      * connection is ready to be accepted, it should return EAGAIN.
116      *
117      * Nonnull iff this is a passive vconn (one that accepts connection and
118      * does not transfer data). */
119     int (*accept)(struct vconn *vconn, struct vconn **new_vconnp);
120
121     /* Tries to receive an OpenFlow message from 'vconn', which must be an
122      * active vconn.  If successful, stores the received message into '*msgp'
123      * and returns 0.  The caller is responsible for destroying the message
124      * with buffer_delete().  On failure, returns a positive errno value and
125      * stores a null pointer into '*msgp'.
126      *
127      * If the connection has been closed in the normal fashion, returns EOF.
128      *
129      * The recv function must not block waiting for a packet to arrive.  If no
130      * packets have been received, it should return EAGAIN.
131      *
132      * Nonnull iff this is an active vconn (one that transfers data and does
133      * not accept connections). */
134     int (*recv)(struct vconn *vconn, struct buffer **msgp);
135
136     /* Tries to queue 'msg' for transmission on 'vconn', which must be an
137      * active vconn.  If successful, returns 0, in which case ownership of
138      * 'msg' is transferred to the vconn.  Success does not guarantee that
139      * 'msg' has been or ever will be delivered to the peer, only that it has
140      * been queued for transmission.
141      *
142      * Returns a positive errno value on failure, in which case the caller
143      * retains ownership of 'msg'.
144      *
145      * The send function must not block.  If 'msg' cannot be immediately
146      * accepted for transmission, it should return EAGAIN.
147      *
148      * Nonnull iff this is an active vconn (one that transfers data and does
149      * not accept connections). */
150     int (*send)(struct vconn *vconn, struct buffer *msg);
151 };
152
153 extern struct vconn_class tcp_vconn_class;
154 extern struct vconn_class ptcp_vconn_class;
155 #ifdef HAVE_OPENSSL
156 extern struct vconn_class ssl_vconn_class;
157 extern struct vconn_class pssl_vconn_class;
158 #endif
159 #ifdef HAVE_NETLINK
160 extern struct vconn_class netlink_vconn_class;
161 #endif
162
163 #endif /* vconn.h */