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[] = {
46 /* Check the validity of the vconn class structures. */
48 check_vconn_classes(void)
53 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
54 struct vconn_class *class = vconn_classes[i];
55 assert(class->name != NULL);
56 assert(class->open != NULL);
57 assert(class->close != NULL);
58 assert(class->prepoll != NULL);
60 ? !class->recv && !class->send
61 : class->recv && class->send);
66 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
67 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
68 * vconn class-specific.
70 * Returns 0 if successful, otherwise a positive errno value. If successful,
71 * stores a pointer to the new connection in '*vconnp', otherwise a null
74 vconn_open(const char *name, struct vconn **vconnp)
79 check_vconn_classes();
81 prefix_len = strcspn(name, ":");
82 if (prefix_len == strlen(name)) {
83 fatal(0, "`%s' not correct format for peer name", name);
85 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
86 struct vconn_class *class = vconn_classes[i];
87 if (strlen(class->name) == prefix_len
88 && !memcmp(class->name, name, prefix_len)) {
89 char *suffix_copy = xstrdup(name + prefix_len + 1);
90 int retval = class->open(name, suffix_copy, vconnp);
98 fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
102 /* Closes 'vconn'. */
104 vconn_close(struct vconn *vconn)
107 (vconn->class->close)(vconn);
111 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
112 * wait for connections to arrive, not to transfer data. Returns false if
113 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
114 * to wait for new connections to arrive. */
116 vconn_is_passive(const struct vconn *vconn)
118 return vconn->class->accept != NULL;
121 /* Initializes 'pfd->fd' and 'pfd->events' appropriately so that poll() will
122 * wake up when the connection becomes available for the operations specified
123 * in 'want', or for performing the vconn's needed internal processing.
125 * Normally returns false. Returns true to indicate that no blocking should
126 * happen in poll() because the connection is available for some operation
127 * specified in 'want' but that status cannot be detected via poll() and thus
128 * poll() could block forever otherwise. */
130 vconn_prepoll(struct vconn *vconn, int want, struct pollfd *pollfd)
132 return (vconn->class->prepoll)(vconn, want, pollfd);
135 /* Perform any internal processing needed by the connections. The vconn file
136 * descriptor's status, as reported by poll(), must be provided in '*revents'.
138 * The postpoll function adjusts '*revents' to reflect the status of the
139 * connection from the caller's point of view. That is, upon return '*revents
140 * & POLLIN' indicates that a packet is (potentially) ready to be read (for an
141 * active vconn) or a new connection is ready to be accepted (for a passive
142 * vconn) and '*revents & POLLOUT' indicates that a packet is (potentially)
143 * ready to be written. */
145 vconn_postpoll(struct vconn *vconn, short int *revents)
147 if (vconn->class->postpoll) {
148 (vconn->class->postpoll)(vconn, revents);
152 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
153 * If successful, stores the new connection in '*new_vconn' and returns 0.
154 * Otherwise, returns a positive errno value.
156 * vconn_accept will not block waiting for a connection. If no connection is
157 * ready to be accepted, it returns EAGAIN immediately. */
159 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
161 int retval = (vconn->class->accept)(vconn, new_vconn);
168 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
169 * vconn. If successful, stores the received message into '*msgp' and returns
170 * 0. The caller is responsible for destroying the message with
171 * buffer_delete(). On failure, returns a positive errno value and stores a
172 * null pointer into '*msgp'. On normal connection close, returns EOF.
174 * vconn_recv will not block waiting for a packet to arrive. If no packets
175 * have been received, it returns EAGAIN immediately. */
177 vconn_recv(struct vconn *vconn, struct buffer **msgp)
179 int retval = (vconn->class->recv)(vconn, msgp);
186 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
187 * vconn. If successful, returns 0, in which case ownership of 'msg' is
188 * transferred to the vconn. Success does not guarantee that 'msg' has been or
189 * ever will be delivered to the peer, only that it has been queued for
192 * Returns a positive errno value on failure, in which case the caller
193 * retains ownership of 'msg'.
195 * vconn_send will not block. If 'msg' cannot be immediately accepted for
196 * transmission, it returns EAGAIN immediately. */
198 vconn_send(struct vconn *vconn, struct buffer *msg)
200 return (vconn->class->send)(vconn, msg);
203 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
205 vconn_send_wait(struct vconn *vconn, struct buffer *msg)
208 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
213 vconn_prepoll(vconn, WANT_SEND, &pfd);
215 retval = poll(&pfd, 1, -1);
216 } while (retval < 0 && errno == EINTR);
221 vconn_postpoll(vconn, &pfd.revents);
227 make_add_simple_flow(const struct flow *flow,
228 uint32_t buffer_id, uint16_t out_port)
230 struct ofp_flow_mod *ofm;
231 size_t size = sizeof *ofm + sizeof ofm->actions[0];
232 struct buffer *out = buffer_new(size);
233 ofm = buffer_put_uninit(out, size);
234 memset(ofm, 0, size);
235 ofm->header.version = OFP_VERSION;
236 ofm->header.type = OFPT_FLOW_MOD;
237 ofm->header.length = htons(size);
238 ofm->match.wildcards = htons(0);
239 ofm->match.in_port = flow->in_port;
240 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
241 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
242 ofm->match.dl_vlan = flow->dl_vlan;
243 ofm->match.dl_type = flow->dl_type;
244 ofm->match.nw_src = flow->nw_src;
245 ofm->match.nw_dst = flow->nw_dst;
246 ofm->match.nw_proto = flow->nw_proto;
247 ofm->match.tp_src = flow->tp_src;
248 ofm->match.tp_dst = flow->tp_dst;
249 ofm->command = htons(OFPFC_ADD);
250 ofm->max_idle = htons(60);
251 ofm->buffer_id = htonl(buffer_id);
252 ofm->group_id = htonl(0);
253 ofm->actions[0].type = htons(OFPAT_OUTPUT);
254 ofm->actions[0].arg.output.max_len = htons(0);
255 ofm->actions[0].arg.output.port = htons(out_port);
260 make_unbuffered_packet_out(const struct buffer *packet,
261 uint16_t in_port, uint16_t out_port)
263 struct ofp_packet_out *opo;
264 size_t size = sizeof *opo + packet->size;
265 struct buffer *out = buffer_new(size);
266 opo = buffer_put_uninit(out, size);
267 memset(opo, 0, sizeof *opo);
268 opo->header.version = OFP_VERSION;
269 opo->header.type = OFPT_PACKET_OUT;
270 opo->header.length = htons(size);
271 opo->buffer_id = htonl(UINT32_MAX);
272 opo->in_port = htons(in_port);
273 opo->out_port = htons(out_port);
274 memcpy(opo->u.data, packet->data, packet->size);
279 make_buffered_packet_out(uint32_t buffer_id,
280 uint16_t in_port, uint16_t out_port)
282 struct ofp_packet_out *opo;
283 size_t size = sizeof *opo + sizeof opo->u.actions[0];
284 struct buffer *out = buffer_new(size);
285 opo = buffer_put_uninit(out, size);
286 memset(opo, 0, size);
287 opo->header.version = OFP_VERSION;
288 opo->header.type = OFPT_PACKET_OUT;
289 opo->header.length = htons(size);
290 opo->buffer_id = htonl(buffer_id);
291 opo->in_port = htons(in_port);
292 opo->out_port = htons(out_port);
293 opo->u.actions[0].type = htons(OFPAT_OUTPUT);
294 opo->u.actions[0].arg.output.max_len = htons(0);
295 opo->u.actions[0].arg.output.port = htons(out_port);