openflow: Change ofp_phy_port's 'name' member from uint8_t[] to char[].
[cascardo/ovs.git] / lib / learning-switch.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "learning-switch.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "byte-order.h"
27 #include "classifier.h"
28 #include "flow.h"
29 #include "hmap.h"
30 #include "mac-learning.h"
31 #include "ofpbuf.h"
32 #include "ofp-parse.h"
33 #include "ofp-print.h"
34 #include "ofp-util.h"
35 #include "openflow/openflow.h"
36 #include "poll-loop.h"
37 #include "queue.h"
38 #include "rconn.h"
39 #include "shash.h"
40 #include "timeval.h"
41 #include "vconn.h"
42 #include "vlog.h"
43
44 VLOG_DEFINE_THIS_MODULE(learning_switch);
45
46 struct lswitch_port {
47     struct hmap_node hmap_node; /* Hash node for port number. */
48     uint16_t port_no;           /* OpenFlow port number, in host byte order. */
49     uint32_t queue_id;          /* OpenFlow queue number. */
50 };
51
52 struct lswitch {
53     /* If nonnegative, the switch sets up flows that expire after the given
54      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
55      * Otherwise, the switch processes every packet. */
56     int max_idle;
57
58     unsigned long long int datapath_id;
59     time_t last_features_request;
60     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
61     struct flow_wildcards wc;   /* Wildcards to apply to flows. */
62     bool action_normal;         /* Use OFPP_NORMAL? */
63
64     /* Queue distribution. */
65     uint32_t default_queue;     /* Default OpenFlow queue, or UINT32_MAX. */
66     struct hmap queue_numbers;  /* Map from port number to lswitch_port. */
67     struct shash queue_names;   /* Map from port name to lswitch_port. */
68
69     /* Number of outgoing queued packets on the rconn. */
70     struct rconn_packet_counter *queued;
71 };
72
73 /* The log messages here could actually be useful in debugging, so keep the
74  * rate limit relatively high. */
75 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
76
77 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
78 static void send_features_request(struct lswitch *, struct rconn *);
79
80 typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
81 static packet_handler_func process_switch_features;
82 static packet_handler_func process_packet_in;
83 static packet_handler_func process_echo_request;
84
85 /* Creates and returns a new learning switch whose configuration is given by
86  * 'cfg'.
87  *
88  * 'rconn' is used to send out an OpenFlow features request. */
89 struct lswitch *
90 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
91 {
92     const struct ofpbuf *b;
93     struct lswitch *sw;
94
95     sw = xzalloc(sizeof *sw);
96     sw->max_idle = cfg->max_idle;
97     sw->datapath_id = 0;
98     sw->last_features_request = time_now() - 1;
99     sw->ml = cfg->mode == LSW_LEARN ? mac_learning_create() : NULL;
100     sw->action_normal = cfg->mode == LSW_NORMAL;
101
102     flow_wildcards_init_exact(&sw->wc);
103     if (!cfg->exact_flows) {
104         /* We cannot wildcard all fields.
105          * We need in_port to detect moves.
106          * We need both SA and DA to do learning. */
107         sw->wc.wildcards = (FWW_DL_TYPE | FWW_NW_PROTO
108                             | FWW_TP_SRC | FWW_TP_DST);
109         sw->wc.nw_src_mask = htonl(0);
110         sw->wc.nw_dst_mask = htonl(0);
111     }
112
113     sw->default_queue = cfg->default_queue;
114     hmap_init(&sw->queue_numbers);
115     shash_init(&sw->queue_names);
116     if (cfg->port_queues) {
117         struct shash_node *node;
118
119         SHASH_FOR_EACH (node, cfg->port_queues) {
120             struct lswitch_port *port = xmalloc(sizeof *port);
121             hmap_node_nullify(&port->hmap_node);
122             port->queue_id = (uintptr_t) node->data;
123             shash_add(&sw->queue_names, node->name, port);
124         }
125     }
126
127     sw->queued = rconn_packet_counter_create();
128     send_features_request(sw, rconn);
129
130     for (b = cfg->default_flows; b; b = b->next) {
131         queue_tx(sw, rconn, ofpbuf_clone(b));
132     }
133
134     return sw;
135 }
136
137 /* Destroys 'sw'. */
138 void
139 lswitch_destroy(struct lswitch *sw)
140 {
141     if (sw) {
142         struct lswitch_port *node, *next;
143
144         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
145             hmap_remove(&sw->queue_numbers, &node->hmap_node);
146             free(node);
147         }
148         shash_destroy(&sw->queue_names);
149         mac_learning_destroy(sw->ml);
150         rconn_packet_counter_destroy(sw->queued);
151         free(sw);
152     }
153 }
154
155 /* Takes care of necessary 'sw' activity, except for receiving packets (which
156  * the caller must do). */
157 void
158 lswitch_run(struct lswitch *sw)
159 {
160     if (sw->ml) {
161         mac_learning_run(sw->ml, NULL);
162     }
163 }
164
165 void
166 lswitch_wait(struct lswitch *sw)
167 {
168     if (sw->ml) {
169         mac_learning_wait(sw->ml);
170     }
171 }
172
173 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
174  * to the learning switch state in 'sw'.  The most likely result of processing
175  * is that flow-setup and packet-out OpenFlow messages will be sent out on
176  * 'rconn'.  */
177 void
178 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
179                        const struct ofpbuf *msg)
180 {
181     struct processor {
182         uint8_t type;
183         size_t min_size;
184         packet_handler_func *handler;
185     };
186     static const struct processor processors[] = {
187         {
188             OFPT_ECHO_REQUEST,
189             sizeof(struct ofp_header),
190             process_echo_request
191         },
192         {
193             OFPT_FEATURES_REPLY,
194             sizeof(struct ofp_switch_features),
195             process_switch_features
196         },
197         {
198             OFPT_PACKET_IN,
199             offsetof(struct ofp_packet_in, data),
200             process_packet_in
201         },
202         {
203             OFPT_FLOW_REMOVED,
204             sizeof(struct ofp_flow_removed),
205             NULL
206         },
207     };
208     const size_t n_processors = ARRAY_SIZE(processors);
209     const struct processor *p;
210     struct ofp_header *oh;
211
212     oh = msg->data;
213     if (sw->datapath_id == 0
214         && oh->type != OFPT_ECHO_REQUEST
215         && oh->type != OFPT_FEATURES_REPLY) {
216         send_features_request(sw, rconn);
217         return;
218     }
219
220     for (p = processors; p < &processors[n_processors]; p++) {
221         if (oh->type == p->type) {
222             if (msg->size < p->min_size) {
223                 VLOG_WARN_RL(&rl, "%016llx: %s: too short (%zu bytes) for "
224                              "type %"PRIu8" (min %zu)", sw->datapath_id,
225                              rconn_get_name(rconn), msg->size, oh->type,
226                              p->min_size);
227                 return;
228             }
229             if (p->handler) {
230                 (p->handler)(sw, rconn, msg->data);
231             }
232             return;
233         }
234     }
235     if (VLOG_IS_DBG_ENABLED()) {
236         char *s = ofp_to_string(msg->data, msg->size, 2);
237         VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
238                     sw->datapath_id, s);
239         free(s);
240     }
241 }
242 \f
243 static void
244 send_features_request(struct lswitch *sw, struct rconn *rconn)
245 {
246     time_t now = time_now();
247     if (now >= sw->last_features_request + 1) {
248         struct ofpbuf *b;
249         struct ofp_switch_config *osc;
250
251         /* Send OFPT_FEATURES_REQUEST. */
252         make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
253         queue_tx(sw, rconn, b);
254
255         /* Send OFPT_SET_CONFIG. */
256         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
257         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
258         queue_tx(sw, rconn, b);
259
260         sw->last_features_request = now;
261     }
262 }
263
264 static void
265 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
266 {
267     int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
268     if (retval && retval != ENOTCONN) {
269         if (retval == EAGAIN) {
270             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
271                          sw->datapath_id, rconn_get_name(rconn));
272         } else {
273             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
274                          sw->datapath_id, rconn_get_name(rconn),
275                          strerror(retval));
276         }
277     }
278 }
279
280 static void
281 process_switch_features(struct lswitch *sw, struct rconn *rconn OVS_UNUSED,
282                         void *osf_)
283 {
284     struct ofp_switch_features *osf = osf_;
285     size_t n_ports;
286     size_t i;
287
288     if (check_ofp_message_array(&osf->header, OFPT_FEATURES_REPLY,
289                                 sizeof *osf, sizeof *osf->ports, &n_ports)) {
290         return;
291     }
292
293     sw->datapath_id = ntohll(osf->datapath_id);
294
295     for (i = 0; i < n_ports; i++) {
296         struct ofp_phy_port *opp = &osf->ports[i];
297         struct lswitch_port *lp;
298
299         opp->name[OFP_MAX_PORT_NAME_LEN - 1] = '\0';
300         lp = shash_find_data(&sw->queue_names, opp->name);
301         if (lp && hmap_node_is_null(&lp->hmap_node)) {
302             lp->port_no = ntohs(opp->port_no);
303             hmap_insert(&sw->queue_numbers, &lp->hmap_node,
304                         hash_int(lp->port_no, 0));
305         }
306     }
307 }
308
309 static uint16_t
310 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
311 {
312     uint16_t out_port;
313
314     /* Learn the source MAC. */
315     if (sw->ml) {
316         if (mac_learning_learn(sw->ml, flow->dl_src, 0, flow->in_port,
317                                GRAT_ARP_LOCK_NONE)) {
318             VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
319                         "port %"PRIu16, sw->datapath_id,
320                         ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
321         }
322     }
323
324     /* Drop frames for reserved multicast addresses. */
325     if (eth_addr_is_reserved(flow->dl_dst)) {
326         return OFPP_NONE;
327     }
328
329     out_port = OFPP_FLOOD;
330     if (sw->ml) {
331         int learned_port = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
332         if (learned_port >= 0) {
333             out_port = learned_port;
334             if (out_port == flow->in_port) {
335                 /* Don't send a packet back out its input port. */
336                 return OFPP_NONE;
337             }
338         }
339     }
340
341     /* Check if we need to use "NORMAL" action. */
342     if (sw->action_normal && out_port != OFPP_FLOOD) {
343         return OFPP_NORMAL;
344     }
345
346     return out_port;
347 }
348
349 static uint32_t
350 get_queue_id(const struct lswitch *sw, uint16_t in_port)
351 {
352     const struct lswitch_port *port;
353
354     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_int(in_port, 0),
355                              &sw->queue_numbers) {
356         if (port->port_no == in_port) {
357             return port->queue_id;
358         }
359     }
360
361     return sw->default_queue;
362 }
363
364 static void
365 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
366 {
367     struct ofp_packet_in *opi = opi_;
368     uint16_t in_port = ntohs(opi->in_port);
369     uint32_t queue_id;
370     uint16_t out_port;
371
372     struct ofp_action_header actions[2];
373     size_t actions_len;
374
375     size_t pkt_ofs, pkt_len;
376     struct ofpbuf pkt;
377     struct flow flow;
378
379     /* Ignore packets sent via output to OFPP_CONTROLLER.  This library never
380      * uses such an action.  You never know what experiments might be going on,
381      * though, and it seems best not to interfere with them. */
382     if (opi->reason != OFPR_NO_MATCH) {
383         return;
384     }
385
386     /* Extract flow data from 'opi' into 'flow'. */
387     pkt_ofs = offsetof(struct ofp_packet_in, data);
388     pkt_len = ntohs(opi->header.length) - pkt_ofs;
389     pkt.data = opi->data;
390     pkt.size = pkt_len;
391     flow_extract(&pkt, 0, in_port, &flow);
392
393     /* Choose output port. */
394     out_port = lswitch_choose_destination(sw, &flow);
395
396     /* Make actions. */
397     queue_id = get_queue_id(sw, in_port);
398     if (out_port == OFPP_NONE) {
399         actions_len = 0;
400     } else if (queue_id == UINT32_MAX || out_port >= OFPP_MAX) {
401         struct ofp_action_output oao;
402
403         memset(&oao, 0, sizeof oao);
404         oao.type = htons(OFPAT_OUTPUT);
405         oao.len = htons(sizeof oao);
406         oao.port = htons(out_port);
407
408         memcpy(actions, &oao, sizeof oao);
409         actions_len = sizeof oao;
410     } else {
411         struct ofp_action_enqueue oae;
412
413         memset(&oae, 0, sizeof oae);
414         oae.type = htons(OFPAT_ENQUEUE);
415         oae.len = htons(sizeof oae);
416         oae.port = htons(out_port);
417         oae.queue_id = htonl(queue_id);
418
419         memcpy(actions, &oae, sizeof oae);
420         actions_len = sizeof oae;
421     }
422     assert(actions_len <= sizeof actions);
423
424     /* Send the packet, and possibly the whole flow, to the output port. */
425     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
426         struct ofpbuf *buffer;
427         struct ofp_flow_mod *ofm;
428         struct cls_rule rule;
429
430         /* The output port is known, or we always flood everything, so add a
431          * new flow. */
432         cls_rule_init(&flow, &sw->wc, 0, &rule);
433         buffer = make_add_flow(&rule, ntohl(opi->buffer_id),
434                                sw->max_idle, actions_len);
435         ofpbuf_put(buffer, actions, actions_len);
436         ofm = buffer->data;
437         queue_tx(sw, rconn, buffer);
438
439         /* If the switch didn't buffer the packet, we need to send a copy. */
440         if (ntohl(opi->buffer_id) == UINT32_MAX && actions_len > 0) {
441             queue_tx(sw, rconn,
442                      make_packet_out(&pkt, UINT32_MAX, in_port,
443                                      actions, actions_len / sizeof *actions));
444         }
445     } else {
446         /* We don't know that MAC, or we don't set up flows.  Send along the
447          * packet without setting up a flow. */
448         if (ntohl(opi->buffer_id) != UINT32_MAX || actions_len > 0) {
449             queue_tx(sw, rconn,
450                      make_packet_out(&pkt, ntohl(opi->buffer_id), in_port,
451                                      actions, actions_len / sizeof *actions));
452         }
453     }
454 }
455
456 static void
457 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
458 {
459     struct ofp_header *rq = rq_;
460     queue_tx(sw, rconn, make_echo_reply(rq));
461 }