1 /* Copyright (C) 2008 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
23 #include <arpa/inet.h>
30 #include "controller.h"
35 #include "poll-loop.h"
39 #define THIS_MODULE VLM_datapath
42 #define BRIDGE_PORT_NO_FLOOD 0x00000001
44 static void send_port_status(struct sw_port *p, uint8_t status);
45 static void del_switch_port(struct sw_port *p);
46 static int port_no(struct datapath *dp, struct sw_port *p)
48 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
52 /* Generates a unique datapath id. It incorporates the datapath index
53 * and a hardware address, if available. If not, it generates a random
59 /* Choose a random datapath id. */
65 for (i = 0; i < ETH_ADDR_LEN; i++) {
66 id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
73 dp_new(struct datapath **dp_, uint64_t dpid, struct controller_connection *cc)
77 dp = calloc(1, sizeof *dp);
82 dp->last_timeout = time(0);
84 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
85 dp->chain = chain_create();
87 VLOG_ERR("could not create chain");
92 list_init(&dp->port_list);
93 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
99 dp_add_port(struct datapath *dp, const char *name)
101 struct netdev *netdev;
105 error = netdev_open(name, &netdev);
110 for (p = dp->ports; ; p++) {
111 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
113 } else if (!p->netdev) {
120 list_push_back(&dp->port_list, &p->node);
122 /* Notify the ctlpath that this port has been added */
123 send_port_status(p, OFPPR_ADD);
129 dp_run(struct datapath *dp)
131 time_t now = time(0);
132 struct sw_port *p, *n;
133 struct buffer *buffer = NULL;
135 if (now != dp->last_timeout) {
136 chain_timeout(dp->chain, dp);
137 dp->last_timeout = now;
139 poll_timer_wait(1000);
141 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
145 /* Allocate buffer with some headroom to add headers in forwarding
146 * to the controller or adding a vlan tag, plus an extra 2 bytes to
147 * allow IP headers to be aligned on a 4-byte boundary. */
148 const int headroom = 128 + 2;
149 buffer = buffer_new(ETH_TOTAL_MAX + headroom);
150 buffer->data += headroom;
152 error = netdev_recv(p->netdev, buffer, false);
154 fwd_port_input(dp, buffer, port_no(dp, p));
156 } else if (error != EAGAIN) {
157 VLOG_ERR("Error receiving data from %s: %s",
158 netdev_get_name(p->netdev), strerror(error));
162 buffer_delete(buffer);
166 dp_wait(struct datapath *dp)
170 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
171 poll_fd_wait(netdev_get_fd(p->netdev), POLLIN, NULL);
175 /* Delete 'p' from switch. */
177 del_switch_port(struct sw_port *p)
179 send_port_status(p, OFPPR_DELETE);
180 netdev_close(p->netdev);
182 list_remove(&p->node);
186 dp_destroy(struct datapath *dp)
188 struct sw_port *p, *n;
194 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
197 chain_destroy(dp->chain);
202 flood(struct datapath *dp, struct buffer *buffer, int in_port)
205 struct sw_port *prev_port;
208 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
209 if (port_no(dp, p) == in_port || p->flags & BRIDGE_PORT_NO_FLOOD) {
213 struct buffer *clone = buffer_clone(buffer);
215 buffer_delete(buffer);
218 dp_output_port(dp, clone, in_port, port_no(dp, prev_port));
223 dp_output_port(dp, buffer, in_port, port_no(dp, prev_port));
225 buffer_delete(buffer);
231 output_packet(struct datapath *dp, struct buffer *buffer, int out_port)
233 if (out_port >= 0 && out_port < OFPP_MAX) {
234 struct sw_port *p = &dp->ports[out_port];
235 if (p->netdev != NULL) {
236 /* FIXME: queue packets. */
237 netdev_send(p->netdev, buffer, false);
242 buffer_delete(buffer);
243 /* FIXME: ratelimit */
244 VLOG_DBG("can't forward to bad port %d\n", out_port);
247 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
250 dp_output_port(struct datapath *dp, struct buffer *buffer,
251 int in_port, int out_port)
255 if (out_port == OFPP_FLOOD) {
256 flood(dp, buffer, in_port);
257 } else if (out_port == OFPP_CONTROLLER) {
258 dp_output_control(dp, buffer, in_port, fwd_save_buffer(buffer), 0,
261 output_packet(dp, buffer, out_port);
265 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If
266 * 'buffer_id' != -1, then only the first 64 bytes of 'buffer' are sent;
267 * otherwise, all of 'buffer' is sent. 'reason' indicates why 'buffer' is
268 * being sent. 'max_len' sets the maximum number of bytes that the caller wants
269 * to be sent; a value of 0 indicates the entire packet should be sent. */
271 dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
272 uint32_t buffer_id, size_t max_len, int reason)
274 struct ofp_packet_in *opi;
277 total_len = buffer->size;
278 if (buffer_id != UINT32_MAX && max_len > buffer->size) {
279 buffer->size = max_len;
282 opi = buffer_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
283 opi->header.version = OFP_VERSION;
284 opi->header.type = OFPT_PACKET_IN;
285 opi->header.length = htons(buffer->size);
286 opi->header.xid = htonl(0);
287 opi->buffer_id = htonl(buffer_id);
288 opi->total_len = htons(total_len);
289 opi->in_port = htons(in_port);
290 opi->reason = reason;
292 controller_send(dp->cc, buffer);
295 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
296 struct ofp_phy_port *desc)
298 desc->port_no = htons(port_no(dp, p));
299 strncpy((char *) desc->name, netdev_get_name(p->netdev),
301 desc->name[sizeof desc->name - 1] = '\0';
302 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
303 desc->flags = htonl(p->flags);
304 desc->features = htonl(netdev_get_features(p->netdev));
305 desc->speed = htonl(netdev_get_speed(p->netdev));
309 dp_send_hello(struct datapath *dp)
311 struct buffer *buffer;
312 struct ofp_data_hello *odh;
315 buffer = buffer_new(sizeof *odh);
316 odh = buffer_put_uninit(buffer, sizeof *odh);
317 memset(odh, 0, sizeof *odh);
318 odh->header.version = OFP_VERSION;
319 odh->header.type = OFPT_DATA_HELLO;
320 odh->header.xid = htonl(0);
321 odh->datapath_id = htonll(dp->id);
322 odh->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
323 odh->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
324 odh->n_compression = 0; /* Not supported */
325 odh->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
326 odh->buffer_mb = htonl(UINT32_MAX);
327 odh->n_buffers = htonl(N_PKT_BUFFERS);
328 odh->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
329 odh->actions = htonl(OFP_SUPPORTED_ACTIONS);
330 odh->miss_send_len = htons(dp->miss_send_len);
331 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
332 struct ofp_phy_port *opp = buffer_put_uninit(buffer, sizeof *opp);
333 memset(opp, 0, sizeof *opp);
334 fill_port_desc(dp, p, opp);
336 odh = buffer_at_assert(buffer, 0, sizeof *odh);
337 odh->header.length = htons(buffer->size);
338 controller_send(dp->cc, buffer);
342 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
346 p = &dp->ports[htons(opp->port_no)];
348 /* Make sure the port id hasn't changed since this was sent */
349 if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
353 p->flags = htonl(opp->flags);
357 send_port_status(struct sw_port *p, uint8_t status)
359 struct buffer *buffer;
360 struct ofp_port_status *ops;
361 buffer = buffer_new(sizeof *ops);
362 ops = buffer_put_uninit(buffer, sizeof *ops);
363 ops->header.version = OFP_VERSION;
364 ops->header.type = OFPT_PORT_STATUS;
365 ops->header.length = htons(sizeof(*ops));
366 ops->header.xid = htonl(0);
367 ops->reason = status;
368 fill_port_desc(p->dp, p, &ops->desc);
369 controller_send(p->dp->cc, buffer);
373 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
375 struct buffer *buffer;
376 struct ofp_flow_expired *ofe;
377 buffer = buffer_new(sizeof *ofe);
378 ofe = buffer_put_uninit(buffer, sizeof *ofe);
379 ofe->header.version = OFP_VERSION;
380 ofe->header.type = OFPT_FLOW_EXPIRED;
381 ofe->header.length = htons(sizeof(*ofe));
382 ofe->header.xid = htonl(0);
383 flow_fill_match(&ofe->match, &flow->key);
384 ofe->duration = htonl(flow->timeout - flow->max_idle - flow->created);
385 ofe->packet_count = htonll(flow->packet_count);
386 ofe->byte_count = htonll(flow->byte_count);
387 controller_send(dp->cc, buffer);