1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
25 #include <netinet/in.h>
34 static struct vconn_class *vconn_classes[] = {
42 /* Check the validity of the vconn class structures. */
44 check_vconn_classes(void)
49 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
50 struct vconn_class *class = vconn_classes[i];
51 assert(class->name != NULL);
52 assert(class->open != NULL);
53 assert(class->close != NULL);
54 assert(class->prepoll != NULL);
56 ? !class->recv && !class->send
57 : class->recv && class->send);
62 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
63 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
64 * vconn class-specific.
66 * Returns 0 if successful, otherwise a positive errno value. If successful,
67 * stores a pointer to the new connection in '*vconnp', otherwise a null
70 vconn_open(const char *name, struct vconn **vconnp)
75 check_vconn_classes();
77 prefix_len = strcspn(name, ":");
78 if (prefix_len == strlen(name)) {
79 fatal(0, "`%s' not correct format for peer name", name);
81 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
82 struct vconn_class *class = vconn_classes[i];
83 if (strlen(class->name) == prefix_len
84 && !memcmp(class->name, name, prefix_len)) {
85 char *suffix_copy = xstrdup(name + prefix_len + 1);
86 int retval = class->open(name, suffix_copy, vconnp);
94 fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
100 vconn_close(struct vconn *vconn)
103 (vconn->class->close)(vconn);
107 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
108 * wait for connections to arrive, not to transfer data. Returns false if
109 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
110 * to wait for new connections to arrive. */
112 vconn_is_passive(const struct vconn *vconn)
114 return vconn->class->accept != NULL;
117 /* Initializes 'pfd->fd' and 'pfd->events' appropriately so that poll() will
118 * wake up when the connection becomes available for the operations specified
119 * in 'want', or for performing the vconn's needed internal processing. */
121 vconn_prepoll(struct vconn *vconn, int want, struct pollfd *pollfd)
123 (vconn->class->prepoll)(vconn, want, pollfd);
126 /* Perform any internal processing needed by the connections. The vconn file
127 * descriptor's status, as reported by poll(), must be provided in '*revents'.
129 * The postpoll function adjusts '*revents' to reflect the status of the
130 * connection from the caller's point of view. That is, upon return '*revents
131 * & POLLIN' indicates that a packet is (potentially) ready to be read (for an
132 * active vconn) or a new connection is ready to be accepted (for a passive
133 * vconn) and '*revents & POLLOUT' indicates that a packet is (potentially)
134 * ready to be written. */
136 vconn_postpoll(struct vconn *vconn, short int *revents)
138 if (vconn->class->postpoll) {
139 (vconn->class->postpoll)(vconn, revents);
143 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
144 * If successful, stores the new connection in '*new_vconn' and returns 0.
145 * Otherwise, returns a positive errno value.
147 * vconn_accept will not block waiting for a connection. If no connection is
148 * ready to be accepted, it returns EAGAIN immediately. */
150 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
152 int retval = (vconn->class->accept)(vconn, new_vconn);
159 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
160 * vconn. If successful, stores the received message into '*msgp' and returns
161 * 0. The caller is responsible for destroying the message with
162 * buffer_delete(). On failure, returns a positive errno value and stores a
163 * null pointer into '*msgp'. On normal connection close, returns EOF.
165 * vconn_recv will not block waiting for a packet to arrive. If no packets
166 * have been received, it returns EAGAIN immediately. */
168 vconn_recv(struct vconn *vconn, struct buffer **msgp)
170 int retval = (vconn->class->recv)(vconn, msgp);
177 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
178 * vconn. If successful, returns 0, in which case ownership of 'msg' is
179 * transferred to the vconn. Success does not guarantee that 'msg' has been or
180 * ever will be delivered to the peer, only that it has been queued for
183 * Returns a positive errno value on failure, in which case the caller
184 * retains ownership of 'msg'.
186 * vconn_send will not block. If 'msg' cannot be immediately accepted for
187 * transmission, it returns EAGAIN immediately. */
189 vconn_send(struct vconn *vconn, struct buffer *msg)
191 return (vconn->class->send)(vconn, msg);
194 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
196 vconn_send_wait(struct vconn *vconn, struct buffer *msg)
199 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
204 vconn_prepoll(vconn, WANT_SEND, &pfd);
206 retval = poll(&pfd, 1, -1);
207 } while (retval < 0 && errno == EINTR);
212 vconn_postpoll(vconn, &pfd.revents);
218 make_add_simple_flow(const struct flow *flow,
219 uint32_t buffer_id, uint16_t out_port)
221 struct ofp_flow_mod *ofm;
222 size_t size = sizeof *ofm + sizeof ofm->actions[0];
223 struct buffer *out = buffer_new(size);
224 ofm = buffer_put_uninit(out, size);
225 memset(ofm, 0, size);
226 ofm->header.version = OFP_VERSION;
227 ofm->header.type = OFPT_FLOW_MOD;
228 ofm->header.length = htons(size);
229 ofm->match.wildcards = htons(0);
230 ofm->match.in_port = flow->in_port;
231 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
232 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
233 ofm->match.dl_vlan = flow->dl_vlan;
234 ofm->match.dl_type = flow->dl_type;
235 ofm->match.nw_src = flow->nw_src;
236 ofm->match.nw_dst = flow->nw_dst;
237 ofm->match.nw_proto = flow->nw_proto;
238 ofm->match.tp_src = flow->tp_src;
239 ofm->match.tp_dst = flow->tp_dst;
240 ofm->command = htons(OFPFC_ADD);
241 ofm->max_idle = htons(60);
242 ofm->buffer_id = htonl(buffer_id);
243 ofm->group_id = htonl(0);
244 ofm->actions[0].type = htons(OFPAT_OUTPUT);
245 ofm->actions[0].arg.output.max_len = htons(0);
246 ofm->actions[0].arg.output.port = htons(out_port);
251 make_unbuffered_packet_out(const struct buffer *packet,
252 uint16_t in_port, uint16_t out_port)
254 struct ofp_packet_out *opo;
255 size_t size = sizeof *opo + packet->size;
256 struct buffer *out = buffer_new(size);
257 opo = buffer_put_uninit(out, size);
258 memset(opo, 0, sizeof *opo);
259 opo->header.version = OFP_VERSION;
260 opo->header.type = OFPT_PACKET_OUT;
261 opo->header.length = htons(size);
262 opo->buffer_id = htonl(UINT32_MAX);
263 opo->in_port = htons(in_port);
264 opo->out_port = htons(out_port);
265 memcpy(opo->u.data, packet->data, packet->size);
270 make_buffered_packet_out(uint32_t buffer_id,
271 uint16_t in_port, uint16_t out_port)
273 struct ofp_packet_out *opo;
274 size_t size = sizeof *opo + sizeof opo->u.actions[0];
275 struct buffer *out = buffer_new(size);
276 opo = buffer_put_uninit(out, size);
277 memset(opo, 0, size);
278 opo->header.version = OFP_VERSION;
279 opo->header.type = OFPT_PACKET_OUT;
280 opo->header.length = htons(size);
281 opo->buffer_id = htonl(buffer_id);
282 opo->in_port = htons(in_port);
283 opo->out_port = htons(out_port);
284 opo->u.actions[0].type = htons(OFPAT_OUTPUT);
285 opo->u.actions[0].arg.output.max_len = htons(0);
286 opo->u.actions[0].arg.output.port = htons(out_port);