flow: New function hash_ofp_port() and hash_ofp_port().
[sliver-openvswitch.git] / lib / learning-switch.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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-actions.h"
33 #include "ofp-errors.h"
34 #include "ofp-msgs.h"
35 #include "ofp-parse.h"
36 #include "ofp-print.h"
37 #include "ofp-util.h"
38 #include "openflow/openflow.h"
39 #include "poll-loop.h"
40 #include "rconn.h"
41 #include "shash.h"
42 #include "simap.h"
43 #include "timeval.h"
44 #include "vconn.h"
45 #include "vlog.h"
46
47 VLOG_DEFINE_THIS_MODULE(learning_switch);
48
49 struct lswitch_port {
50     struct hmap_node hmap_node; /* Hash node for port number. */
51     ofp_port_t port_no;         /* OpenFlow port number. */
52     uint32_t queue_id;          /* OpenFlow queue number. */
53 };
54
55 enum lswitch_state {
56     S_CONNECTING,               /* Waiting for connection to complete. */
57     S_FEATURES_REPLY,           /* Waiting for features reply. */
58     S_SWITCHING,                /* Switching flows. */
59 };
60
61 struct lswitch {
62     struct rconn *rconn;
63     enum lswitch_state state;
64
65     /* If nonnegative, the switch sets up flows that expire after the given
66      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
67      * Otherwise, the switch processes every packet. */
68     int max_idle;
69
70     enum ofputil_protocol protocol;
71     unsigned long long int datapath_id;
72     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
73     struct flow_wildcards wc;   /* Wildcards to apply to flows. */
74     bool action_normal;         /* Use OFPP_NORMAL? */
75
76     /* Queue distribution. */
77     uint32_t default_queue;     /* Default OpenFlow queue, or UINT32_MAX. */
78     struct hmap queue_numbers;  /* Map from port number to lswitch_port. */
79     struct shash queue_names;   /* Map from port name to lswitch_port. */
80
81     /* Number of outgoing queued packets on the rconn. */
82     struct rconn_packet_counter *queued;
83
84     /* If true, do not reply to any messages from the switch (for debugging
85      * fail-open mode). */
86     bool mute;
87
88     /* Optional "flow mod" requests to send to the switch at connection time,
89      * to set up the flow table. */
90     const struct ofputil_flow_mod *default_flows;
91     size_t n_default_flows;
92 };
93
94 /* The log messages here could actually be useful in debugging, so keep the
95  * rate limit relatively high. */
96 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
97
98 static void queue_tx(struct lswitch *, struct ofpbuf *);
99 static void send_features_request(struct lswitch *);
100
101 static void lswitch_process_packet(struct lswitch *, const struct ofpbuf *);
102 static enum ofperr process_switch_features(struct lswitch *,
103                                            struct ofp_header *);
104 static void process_packet_in(struct lswitch *, const struct ofp_header *);
105 static void process_echo_request(struct lswitch *, const struct ofp_header *);
106
107 /* Creates and returns a new learning switch whose configuration is given by
108  * 'cfg'.
109  *
110  * 'rconn' is used to send out an OpenFlow features request. */
111 struct lswitch *
112 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
113 {
114     struct lswitch *sw;
115     uint32_t ofpfw;
116
117     sw = xzalloc(sizeof *sw);
118     sw->rconn = rconn;
119     sw->state = S_CONNECTING;
120     sw->max_idle = cfg->max_idle;
121     sw->datapath_id = 0;
122     sw->ml = (cfg->mode == LSW_LEARN
123               ? mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME)
124               : NULL);
125     sw->action_normal = cfg->mode == LSW_NORMAL;
126
127     switch (cfg->wildcards) {
128     case 0:
129         ofpfw = 0;
130         break;
131
132     case UINT32_MAX:
133         /* Try to wildcard as many fields as possible, but we cannot
134          * wildcard all fields.  We need in_port to detect moves.  We need
135          * Ethernet source and dest and VLAN VID to do L2 learning. */
136         ofpfw = (OFPFW10_DL_TYPE | OFPFW10_DL_VLAN_PCP
137                  | OFPFW10_NW_SRC_ALL | OFPFW10_NW_DST_ALL
138                  | OFPFW10_NW_TOS | OFPFW10_NW_PROTO
139                  | OFPFW10_TP_SRC | OFPFW10_TP_DST);
140         break;
141
142     default:
143         ofpfw = cfg->wildcards;
144         break;
145     }
146     ofputil_wildcard_from_ofpfw10(ofpfw, &sw->wc);
147
148     sw->default_queue = cfg->default_queue;
149     hmap_init(&sw->queue_numbers);
150     shash_init(&sw->queue_names);
151     if (cfg->port_queues) {
152         struct simap_node *node;
153
154         SIMAP_FOR_EACH (node, cfg->port_queues) {
155             struct lswitch_port *port = xmalloc(sizeof *port);
156             hmap_node_nullify(&port->hmap_node);
157             port->queue_id = node->data;
158             shash_add(&sw->queue_names, node->name, port);
159         }
160     }
161
162     sw->default_flows = cfg->default_flows;
163     sw->n_default_flows = cfg->n_default_flows;
164
165     sw->queued = rconn_packet_counter_create();
166
167     return sw;
168 }
169
170 static void
171 lswitch_handshake(struct lswitch *sw)
172 {
173     enum ofputil_protocol protocol;
174
175     send_features_request(sw);
176
177     protocol = ofputil_protocol_from_ofp_version(rconn_get_version(sw->rconn));
178     if (sw->default_flows) {
179         enum ofputil_protocol usable_protocols;
180         struct ofpbuf *msg = NULL;
181         int error = 0;
182         size_t i;
183
184         /* If the initial protocol isn't good enough for default_flows, then
185          * pick one that will work and encode messages to set up that
186          * protocol.
187          *
188          * This could be improved by actually negotiating a mutually acceptable
189          * flow format with the switch, but that would require an asynchronous
190          * state machine.  This version ought to work fine in practice. */
191         usable_protocols = ofputil_flow_mod_usable_protocols(
192             sw->default_flows, sw->n_default_flows);
193         if (!(protocol & usable_protocols)) {
194             enum ofputil_protocol want = rightmost_1bit(usable_protocols);
195             while (!error) {
196                 msg = ofputil_encode_set_protocol(protocol, want, &protocol);
197                 if (!msg) {
198                     break;
199                 }
200                 error = rconn_send(sw->rconn, msg, NULL);
201             }
202         }
203         if (protocol & usable_protocols) {
204             for (i = 0; !error && i < sw->n_default_flows; i++) {
205                 msg = ofputil_encode_flow_mod(&sw->default_flows[i], protocol);
206                 error = rconn_send(sw->rconn, msg, NULL);
207             }
208
209             if (error) {
210                 VLOG_INFO_RL(&rl, "%s: failed to queue default flows (%s)",
211                              rconn_get_name(sw->rconn), strerror(error));
212             }
213         } else {
214             VLOG_INFO_RL(&rl, "%s: failed to set usable protocol",
215                          rconn_get_name(sw->rconn));
216         }
217     }
218     sw->protocol = protocol;
219 }
220
221 bool
222 lswitch_is_alive(const struct lswitch *sw)
223 {
224     return rconn_is_alive(sw->rconn);
225 }
226
227 /* Destroys 'sw'. */
228 void
229 lswitch_destroy(struct lswitch *sw)
230 {
231     if (sw) {
232         struct lswitch_port *node, *next;
233
234         rconn_destroy(sw->rconn);
235         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
236             hmap_remove(&sw->queue_numbers, &node->hmap_node);
237             free(node);
238         }
239         shash_destroy(&sw->queue_names);
240         mac_learning_destroy(sw->ml);
241         rconn_packet_counter_destroy(sw->queued);
242         free(sw);
243     }
244 }
245
246 /* Takes care of necessary 'sw' activity, except for receiving packets (which
247  * the caller must do). */
248 void
249 lswitch_run(struct lswitch *sw)
250 {
251     int i;
252
253     if (sw->ml) {
254         mac_learning_run(sw->ml, NULL);
255     }
256
257     rconn_run(sw->rconn);
258
259     if (sw->state == S_CONNECTING) {
260         if (rconn_get_version(sw->rconn) != -1) {
261             lswitch_handshake(sw);
262             sw->state = S_FEATURES_REPLY;
263         }
264         return;
265     }
266
267     for (i = 0; i < 50; i++) {
268         struct ofpbuf *msg;
269
270         msg = rconn_recv(sw->rconn);
271         if (!msg) {
272             break;
273         }
274
275         if (!sw->mute) {
276             lswitch_process_packet(sw, msg);
277         }
278         ofpbuf_delete(msg);
279     }
280 }
281
282 void
283 lswitch_wait(struct lswitch *sw)
284 {
285     if (sw->ml) {
286         mac_learning_wait(sw->ml);
287     }
288     rconn_run_wait(sw->rconn);
289     rconn_recv_wait(sw->rconn);
290 }
291
292 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
293  * to the learning switch state in 'sw'.  The most likely result of processing
294  * is that flow-setup and packet-out OpenFlow messages will be sent out on
295  * 'rconn'.  */
296 static void
297 lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
298 {
299     enum ofptype type;
300     struct ofpbuf b;
301
302     b = *msg;
303     if (ofptype_pull(&type, &b)) {
304         return;
305     }
306
307     if (sw->state == S_FEATURES_REPLY
308         && type != OFPTYPE_ECHO_REQUEST
309         && type != OFPTYPE_FEATURES_REPLY) {
310         return;
311     }
312
313     switch (type) {
314     case OFPTYPE_ECHO_REQUEST:
315         process_echo_request(sw, msg->data);
316         break;
317
318     case OFPTYPE_FEATURES_REPLY:
319         if (sw->state == S_FEATURES_REPLY) {
320             if (!process_switch_features(sw, msg->data)) {
321                 sw->state = S_SWITCHING;
322             } else {
323                 rconn_disconnect(sw->rconn);
324             }
325         }
326         break;
327
328     case OFPTYPE_PACKET_IN:
329         process_packet_in(sw, msg->data);
330         break;
331
332     case OFPTYPE_FLOW_REMOVED:
333         /* Nothing to do. */
334         break;
335
336     case OFPTYPE_HELLO:
337     case OFPTYPE_ERROR:
338     case OFPTYPE_ECHO_REPLY:
339     case OFPTYPE_FEATURES_REQUEST:
340     case OFPTYPE_GET_CONFIG_REQUEST:
341     case OFPTYPE_GET_CONFIG_REPLY:
342     case OFPTYPE_SET_CONFIG:
343     case OFPTYPE_PORT_STATUS:
344     case OFPTYPE_PACKET_OUT:
345     case OFPTYPE_FLOW_MOD:
346     case OFPTYPE_PORT_MOD:
347     case OFPTYPE_BARRIER_REQUEST:
348     case OFPTYPE_BARRIER_REPLY:
349     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
350     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
351     case OFPTYPE_DESC_STATS_REQUEST:
352     case OFPTYPE_DESC_STATS_REPLY:
353     case OFPTYPE_FLOW_STATS_REQUEST:
354     case OFPTYPE_FLOW_STATS_REPLY:
355     case OFPTYPE_AGGREGATE_STATS_REQUEST:
356     case OFPTYPE_AGGREGATE_STATS_REPLY:
357     case OFPTYPE_TABLE_STATS_REQUEST:
358     case OFPTYPE_TABLE_STATS_REPLY:
359     case OFPTYPE_PORT_STATS_REQUEST:
360     case OFPTYPE_PORT_STATS_REPLY:
361     case OFPTYPE_QUEUE_STATS_REQUEST:
362     case OFPTYPE_QUEUE_STATS_REPLY:
363     case OFPTYPE_PORT_DESC_STATS_REQUEST:
364     case OFPTYPE_PORT_DESC_STATS_REPLY:
365     case OFPTYPE_ROLE_REQUEST:
366     case OFPTYPE_ROLE_REPLY:
367     case OFPTYPE_SET_FLOW_FORMAT:
368     case OFPTYPE_FLOW_MOD_TABLE_ID:
369     case OFPTYPE_SET_PACKET_IN_FORMAT:
370     case OFPTYPE_FLOW_AGE:
371     case OFPTYPE_SET_CONTROLLER_ID:
372     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
373     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
374     case OFPTYPE_FLOW_MONITOR_CANCEL:
375     case OFPTYPE_FLOW_MONITOR_PAUSED:
376     case OFPTYPE_FLOW_MONITOR_RESUMED:
377     case OFPTYPE_GET_ASYNC_REQUEST:
378     case OFPTYPE_GET_ASYNC_REPLY:
379     case OFPTYPE_SET_ASYNC_CONFIG:
380     case OFPTYPE_METER_MOD:
381     case OFPTYPE_GROUP_REQUEST:
382     case OFPTYPE_GROUP_REPLY:
383     case OFPTYPE_GROUP_DESC_REQUEST:
384     case OFPTYPE_GROUP_DESC_REPLY:
385     case OFPTYPE_GROUP_FEATURES_REQUEST:
386     case OFPTYPE_GROUP_FEATURES_REPLY:
387     case OFPTYPE_METER_REQUEST:
388     case OFPTYPE_METER_REPLY:
389     case OFPTYPE_METER_CONFIG_REQUEST:
390     case OFPTYPE_METER_CONFIG_REPLY:
391     case OFPTYPE_METER_FEATURES_REQUEST:
392     case OFPTYPE_METER_FEATURES_REPLY:
393     case OFPTYPE_TABLE_FEATURES_REQUEST:
394     case OFPTYPE_TABLE_FEATURES_REPLY:
395     default:
396         if (VLOG_IS_DBG_ENABLED()) {
397             char *s = ofp_to_string(msg->data, msg->size, 2);
398             VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
399                         sw->datapath_id, s);
400             free(s);
401         }
402     }
403 }
404 \f
405 static void
406 send_features_request(struct lswitch *sw)
407 {
408     struct ofpbuf *b;
409     struct ofp_switch_config *osc;
410     int ofp_version = rconn_get_version(sw->rconn);
411
412     ovs_assert(ofp_version > 0 && ofp_version < 0xff);
413
414     /* Send OFPT_FEATURES_REQUEST. */
415     b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
416     queue_tx(sw, b);
417
418     /* Send OFPT_SET_CONFIG. */
419     b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, ofp_version, sizeof *osc);
420     osc = ofpbuf_put_zeros(b, sizeof *osc);
421     osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
422     queue_tx(sw, b);
423 }
424
425 static void
426 queue_tx(struct lswitch *sw, struct ofpbuf *b)
427 {
428     int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
429     if (retval && retval != ENOTCONN) {
430         if (retval == EAGAIN) {
431             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
432                          sw->datapath_id, rconn_get_name(sw->rconn));
433         } else {
434             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
435                          sw->datapath_id, rconn_get_name(sw->rconn),
436                          strerror(retval));
437         }
438     }
439 }
440
441 static enum ofperr
442 process_switch_features(struct lswitch *sw, struct ofp_header *oh)
443 {
444     struct ofputil_switch_features features;
445     struct ofputil_phy_port port;
446     enum ofperr error;
447     struct ofpbuf b;
448
449     error = ofputil_decode_switch_features(oh, &features, &b);
450     if (error) {
451         VLOG_ERR("received invalid switch feature reply (%s)",
452                  ofperr_to_string(error));
453         return error;
454     }
455
456     sw->datapath_id = features.datapath_id;
457
458     while (!ofputil_pull_phy_port(oh->version, &b, &port)) {
459         struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
460         if (lp && hmap_node_is_null(&lp->hmap_node)) {
461             lp->port_no = port.port_no;
462             hmap_insert(&sw->queue_numbers, &lp->hmap_node,
463                         hash_ofp_port(lp->port_no));
464         }
465     }
466     return 0;
467 }
468
469 static ofp_port_t
470 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
471 {
472     ofp_port_t out_port;
473
474     /* Learn the source MAC. */
475     if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
476         struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src, 0);
477         if (mac_entry_is_new(mac)
478             || mac->port.ofp_port != flow->in_port.ofp_port) {
479             VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
480                         "port %"PRIu16, sw->datapath_id,
481                         ETH_ADDR_ARGS(flow->dl_src), flow->in_port.ofp_port);
482
483             mac->port.ofp_port = flow->in_port.ofp_port;
484             mac_learning_changed(sw->ml, mac);
485         }
486     }
487
488     /* Drop frames for reserved multicast addresses. */
489     if (eth_addr_is_reserved(flow->dl_dst)) {
490         return OFPP_NONE;
491     }
492
493     out_port = OFPP_FLOOD;
494     if (sw->ml) {
495         struct mac_entry *mac;
496
497         mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
498         if (mac) {
499             out_port = mac->port.ofp_port;
500             if (out_port == flow->in_port.ofp_port) {
501                 /* Don't send a packet back out its input port. */
502                 return OFPP_NONE;
503             }
504         }
505     }
506
507     /* Check if we need to use "NORMAL" action. */
508     if (sw->action_normal && out_port != OFPP_FLOOD) {
509         return OFPP_NORMAL;
510     }
511
512     return out_port;
513 }
514
515 static uint32_t
516 get_queue_id(const struct lswitch *sw, ofp_port_t in_port)
517 {
518     const struct lswitch_port *port;
519
520     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_ofp_port(in_port),
521                              &sw->queue_numbers) {
522         if (port->port_no == in_port) {
523             return port->queue_id;
524         }
525     }
526
527     return sw->default_queue;
528 }
529
530 static void
531 process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
532 {
533     struct ofputil_packet_in pi;
534     uint32_t queue_id;
535     ofp_port_t out_port;
536
537     uint64_t ofpacts_stub[64 / 8];
538     struct ofpbuf ofpacts;
539
540     struct ofputil_packet_out po;
541     enum ofperr error;
542
543     struct ofpbuf pkt;
544     struct flow flow;
545     union flow_in_port in_port_;
546
547     error = ofputil_decode_packet_in(&pi, oh);
548     if (error) {
549         VLOG_WARN_RL(&rl, "failed to decode packet-in: %s",
550                      ofperr_to_string(error));
551         return;
552     }
553
554     /* Ignore packets sent via output to OFPP_CONTROLLER.  This library never
555      * uses such an action.  You never know what experiments might be going on,
556      * though, and it seems best not to interfere with them. */
557     if (pi.reason != OFPR_NO_MATCH) {
558         return;
559     }
560
561     /* Extract flow data from 'opi' into 'flow'. */
562     ofpbuf_use_const(&pkt, pi.packet, pi.packet_len);
563     in_port_.ofp_port = pi.fmd.in_port;
564     flow_extract(&pkt, 0, 0, NULL, &in_port_, &flow);
565     flow.tunnel.tun_id = pi.fmd.tun_id;
566
567     /* Choose output port. */
568     out_port = lswitch_choose_destination(sw, &flow);
569
570     /* Make actions. */
571     queue_id = get_queue_id(sw, pi.fmd.in_port);
572     ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
573     if (out_port == OFPP_NONE) {
574         /* No actions. */
575     } else if (queue_id == UINT32_MAX
576                || ofp_to_u16(out_port) >= ofp_to_u16(OFPP_MAX)) {
577         ofpact_put_OUTPUT(&ofpacts)->port = out_port;
578     } else {
579         struct ofpact_enqueue *enqueue = ofpact_put_ENQUEUE(&ofpacts);
580         enqueue->port = out_port;
581         enqueue->queue = queue_id;
582     }
583     ofpact_pad(&ofpacts);
584
585     /* Prepare packet_out in case we need one. */
586     po.buffer_id = pi.buffer_id;
587     if (po.buffer_id == UINT32_MAX) {
588         po.packet = pkt.data;
589         po.packet_len = pkt.size;
590     } else {
591         po.packet = NULL;
592         po.packet_len = 0;
593     }
594     po.in_port = pi.fmd.in_port;
595     po.ofpacts = ofpacts.data;
596     po.ofpacts_len = ofpacts.size;
597
598     /* Send the packet, and possibly the whole flow, to the output port. */
599     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
600         struct ofputil_flow_mod fm;
601         struct ofpbuf *buffer;
602
603         /* The output port is known, or we always flood everything, so add a
604          * new flow. */
605         memset(&fm, 0, sizeof fm);
606         match_init(&fm.match, &flow, &sw->wc);
607         ofputil_normalize_match_quiet(&fm.match);
608         fm.priority = 0;
609         fm.table_id = 0xff;
610         fm.command = OFPFC_ADD;
611         fm.idle_timeout = sw->max_idle;
612         fm.buffer_id = pi.buffer_id;
613         fm.out_port = OFPP_NONE;
614         fm.ofpacts = ofpacts.data;
615         fm.ofpacts_len = ofpacts.size;
616         buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
617
618         queue_tx(sw, buffer);
619
620         /* If the switch didn't buffer the packet, we need to send a copy. */
621         if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
622             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
623         }
624     } else {
625         /* We don't know that MAC, or we don't set up flows.  Send along the
626          * packet without setting up a flow. */
627         if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
628             queue_tx(sw, ofputil_encode_packet_out(&po, sw->protocol));
629         }
630     }
631 }
632
633 static void
634 process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
635 {
636     queue_tx(sw, make_echo_reply(rq));
637 }