Initial import
[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 void 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     void (*prepoll)(struct vconn *, int want, struct pollfd *pfd);
88
89     /* Called by the main loop after calling poll(), this function may perform
90      * any internal processing needed by the connection.  It is provided with
91      * the vconn file descriptor's status in '*revents', as reported by poll().
92      *
93      * The postpoll function should adjust '*revents' to reflect the status of
94      * the connection from the caller's point of view: that is, upon return
95      * '*revents & POLLIN' should indicate that a packet is (potentially) ready
96      * to be read (for an active vconn) or a new connection is ready to be
97      * accepted (for a passive vconn) and '*revents & POLLOUT' should indicate
98      * that a packet is (potentially) ready to be written.
99      *
100      * This function may be a null pointer in a vconn class that has no use for
101      * it, that is, if the vconn does not need to do any internal processing
102      * and poll's revents out properly reflects the vconn's status.  */
103     void (*postpoll)(struct vconn *, short int *revents);
104
105     /* Tries to accept a new connection on 'vconn', which must be a passive
106      * vconn.  If successful, stores the new connection in '*new_vconnp' and
107      * returns 0.  Otherwise, returns a positive errno value.
108      *
109      * The accept function must not block waiting for a connection.  If no
110      * connection is ready to be accepted, it should return EAGAIN.
111      *
112      * Nonnull iff this is a passive vconn (one that accepts connection and
113      * does not transfer data). */
114     int (*accept)(struct vconn *vconn, struct vconn **new_vconnp);
115
116     /* Tries to receive an OpenFlow message from 'vconn', which must be an
117      * active vconn.  If successful, stores the received message into '*msgp'
118      * and returns 0.  The caller is responsible for destroying the message
119      * with buffer_delete().  On failure, returns a positive errno value and
120      * stores a null pointer into '*msgp'.
121      *
122      * If the connection has been closed in the normal fashion, returns EOF.
123      *
124      * The recv function must not block waiting for a packet to arrive.  If no
125      * packets have been received, it should return EAGAIN.
126      *
127      * Nonnull iff this is an active vconn (one that transfers data and does
128      * not accept connections). */
129     int (*recv)(struct vconn *vconn, struct buffer **msgp);
130
131     /* Tries to queue 'msg' for transmission on 'vconn', which must be an
132      * active vconn.  If successful, returns 0, in which case ownership of
133      * 'msg' is transferred to the vconn.  Success does not guarantee that
134      * 'msg' has been or ever will be delivered to the peer, only that it has
135      * been queued for transmission.
136      *
137      * Returns a positive errno value on failure, in which case the caller
138      * retains ownership of 'msg'.
139      *
140      * The send function must not block.  If 'msg' cannot be immediately
141      * accepted for transmission, it should return EAGAIN.
142      *
143      * Nonnull iff this is an active vconn (one that transfers data and does
144      * not accept connections). */
145     int (*send)(struct vconn *vconn, struct buffer *msg);
146 };
147
148 extern struct vconn_class tcp_vconn_class;
149 extern struct vconn_class ptcp_vconn_class;
150 #ifdef HAVE_NETLINK
151 extern struct vconn_class netlink_vconn_class;
152 #endif
153
154 #endif /* vconn.h */