Implement userspace switch.
[openvswitch] / switch / datapath.c
1 /* Copyright (C) 2008 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 #include "datapath.h"
23 #include <arpa/inet.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "buffer.h"
29 #include "chain.h"
30 #include "controller.h"
31 #include "flow.h"
32 #include "forward.h"
33 #include "netdev.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "table.h"
37 #include "xtoxll.h"
38
39 #define THIS_MODULE VLM_datapath
40 #include "vlog.h"
41
42 #define BRIDGE_PORT_NO_FLOOD    0x00000001
43
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) 
47 {
48     assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
49     return p - dp->ports;
50 }
51
52 /* Generates a unique datapath id.  It incorporates the datapath index
53  * and a hardware address, if available.  If not, it generates a random
54  * one.
55  */
56 static uint64_t
57 gen_datapath_id(void)
58 {
59     /* Choose a random datapath id. */
60     uint64_t id = 0;
61     int i;
62
63     srand(time(0));
64
65     for (i = 0; i < ETH_ADDR_LEN; i++) {
66         id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
67     }
68
69     return id;
70 }
71
72 int
73 dp_new(struct datapath **dp_, uint64_t dpid, struct controller_connection *cc)
74 {
75     struct datapath *dp;
76
77     dp = calloc(1, sizeof *dp);
78     if (!dp) {
79         return ENOMEM;
80     }
81
82     dp->last_timeout = time(0);
83     dp->cc = cc;
84     dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
85     dp->chain = chain_create();
86     if (!dp->chain) {
87         VLOG_ERR("could not create chain");
88         free(dp);
89         return ENOMEM;
90     }
91
92     list_init(&dp->port_list);
93     dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
94     *dp_ = dp;
95     return 0;
96 }
97
98 int
99 dp_add_port(struct datapath *dp, const char *name)
100 {
101     struct netdev *netdev;
102     struct sw_port *p;
103     int error;
104
105     error = netdev_open(name, &netdev);
106     if (error) {
107         return error;
108     }
109
110     for (p = dp->ports; ; p++) {
111         if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
112             return EXFULL;
113         } else if (!p->netdev) {
114             break;
115         }
116     }
117
118     p->dp = dp;
119     p->netdev = netdev;
120     list_push_back(&dp->port_list, &p->node);
121
122     /* Notify the ctlpath that this port has been added */
123     send_port_status(p, OFPPR_ADD);
124
125     return 0;
126 }
127
128 void
129 dp_run(struct datapath *dp) 
130 {
131     time_t now = time(0);
132     struct sw_port *p, *n;
133     struct buffer *buffer = NULL;
134
135     if (now != dp->last_timeout) {
136         chain_timeout(dp->chain, dp);
137         dp->last_timeout = now;
138     }
139     poll_timer_wait(1000);
140     
141     LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
142         int error;
143
144         if (!buffer) {
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;
151         }
152         error = netdev_recv(p->netdev, buffer, false);
153         if (!error) {
154             fwd_port_input(dp, buffer, port_no(dp, p));
155             buffer = NULL;
156         } else if (error != EAGAIN) {
157             VLOG_ERR("Error receiving data from %s: %s",
158                      netdev_get_name(p->netdev), strerror(error));
159             del_switch_port(p);
160         }
161     }
162     buffer_delete(buffer);
163 }
164
165 void
166 dp_wait(struct datapath *dp) 
167 {
168     struct sw_port *p;
169
170     LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
171         poll_fd_wait(netdev_get_fd(p->netdev), POLLIN, NULL);
172     }
173 }
174
175 /* Delete 'p' from switch. */
176 static void
177 del_switch_port(struct sw_port *p)
178 {
179     send_port_status(p, OFPPR_DELETE);
180     netdev_close(p->netdev);
181     p->netdev = NULL;
182     list_remove(&p->node);
183 }
184
185 void
186 dp_destroy(struct datapath *dp)
187 {
188     struct sw_port *p, *n;
189
190     if (!dp) {
191         return;
192     }
193
194     LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
195         del_switch_port(p); 
196     }
197     chain_destroy(dp->chain);
198     free(dp);
199 }
200
201 static int
202 flood(struct datapath *dp, struct buffer *buffer, int in_port)
203 {
204     struct sw_port *p;
205     struct sw_port *prev_port;
206
207     prev_port = NULL;
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) {
210             continue;
211         }
212         if (prev_port) {
213             struct buffer *clone = buffer_clone(buffer);
214             if (!clone) {
215                 buffer_delete(buffer);
216                 return -ENOMEM;
217             }
218             dp_output_port(dp, clone, in_port, port_no(dp, prev_port)); 
219         }
220         prev_port = p;
221     }
222     if (prev_port)
223         dp_output_port(dp, buffer, in_port, port_no(dp, prev_port));
224     else
225         buffer_delete(buffer);
226
227     return 0;
228 }
229
230 void
231 output_packet(struct datapath *dp, struct buffer *buffer, int out_port) 
232 {
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);
238             return;
239         }
240     }
241
242     buffer_delete(buffer);
243     /* FIXME: ratelimit */
244     VLOG_DBG("can't forward to bad port %d\n", out_port);
245 }
246
247 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
248  */
249 void
250 dp_output_port(struct datapath *dp, struct buffer *buffer,
251                int in_port, int out_port)
252 {
253
254     assert(buffer);
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,
259                           OFPR_ACTION); 
260     } else {
261         output_packet(dp, buffer, out_port);
262     }
263 }
264
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. */
270 void
271 dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
272                   uint32_t buffer_id, size_t max_len, int reason)
273 {
274     struct ofp_packet_in *opi;
275     size_t total_len;
276
277     total_len = buffer->size;
278     if (buffer_id != UINT32_MAX && max_len > buffer->size) {
279         buffer->size = max_len;
280     }
281
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;
291     opi->pad            = 0;
292     controller_send(dp->cc, buffer);
293 }
294
295 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
296                            struct ofp_phy_port *desc)
297 {
298     desc->port_no = htons(port_no(dp, p));
299     strncpy((char *) desc->name, netdev_get_name(p->netdev),
300             sizeof desc->name);
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));
306 }
307
308 void
309 dp_send_hello(struct datapath *dp)
310 {
311     struct buffer *buffer;
312     struct ofp_data_hello *odh;
313     struct sw_port *p;
314
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);
335     }
336     odh = buffer_at_assert(buffer, 0, sizeof *odh);
337     odh->header.length = htons(buffer->size);
338     controller_send(dp->cc, buffer);
339 }
340
341 void
342 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
343 {
344     struct sw_port *p;
345
346     p = &dp->ports[htons(opp->port_no)];
347
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),
350                      ETH_ADDR_LEN) != 0) 
351         return;
352         
353     p->flags = htonl(opp->flags);
354 }
355
356 static void
357 send_port_status(struct sw_port *p, uint8_t status) 
358 {
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);
370 }
371
372 void
373 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
374 {
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);
388 }