datapath: Pull data into linear area only on demand.
[sliver-openvswitch.git] / lib / learning-switch.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "rconn.h"
38 #include "shash.h"
39 #include "timeval.h"
40 #include "vconn.h"
41 #include "vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(learning_switch);
44
45 struct lswitch_port {
46     struct hmap_node hmap_node; /* Hash node for port number. */
47     uint16_t port_no;           /* OpenFlow port number, in host byte order. */
48     uint32_t queue_id;          /* OpenFlow queue number. */
49 };
50
51 struct lswitch {
52     /* If nonnegative, the switch sets up flows that expire after the given
53      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
54      * Otherwise, the switch processes every packet. */
55     int max_idle;
56
57     unsigned long long int datapath_id;
58     time_t last_features_request;
59     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
60     struct flow_wildcards wc;   /* Wildcards to apply to flows. */
61     bool action_normal;         /* Use OFPP_NORMAL? */
62
63     /* Queue distribution. */
64     uint32_t default_queue;     /* Default OpenFlow queue, or UINT32_MAX. */
65     struct hmap queue_numbers;  /* Map from port number to lswitch_port. */
66     struct shash queue_names;   /* Map from port name to lswitch_port. */
67
68     /* Number of outgoing queued packets on the rconn. */
69     struct rconn_packet_counter *queued;
70 };
71
72 /* The log messages here could actually be useful in debugging, so keep the
73  * rate limit relatively high. */
74 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
75
76 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
77 static void send_features_request(struct lswitch *, struct rconn *);
78
79 static void process_switch_features(struct lswitch *,
80                                     struct ofp_switch_features *);
81 static void process_packet_in(struct lswitch *, struct rconn *,
82                               const struct ofp_packet_in *);
83 static void process_echo_request(struct lswitch *, struct rconn *,
84                                  const struct ofp_header *);
85
86 /* Creates and returns a new learning switch whose configuration is given by
87  * 'cfg'.
88  *
89  * 'rconn' is used to send out an OpenFlow features request. */
90 struct lswitch *
91 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
92 {
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     if (cfg->default_flows) {
131         const struct ofpbuf *b;
132
133         LIST_FOR_EACH (b, list_node, cfg->default_flows) {
134             queue_tx(sw, rconn, ofpbuf_clone(b));
135         }
136     }
137     
138     return sw;
139 }
140
141 /* Destroys 'sw'. */
142 void
143 lswitch_destroy(struct lswitch *sw)
144 {
145     if (sw) {
146         struct lswitch_port *node, *next;
147
148         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
149             hmap_remove(&sw->queue_numbers, &node->hmap_node);
150             free(node);
151         }
152         shash_destroy(&sw->queue_names);
153         mac_learning_destroy(sw->ml);
154         rconn_packet_counter_destroy(sw->queued);
155         free(sw);
156     }
157 }
158
159 /* Takes care of necessary 'sw' activity, except for receiving packets (which
160  * the caller must do). */
161 void
162 lswitch_run(struct lswitch *sw)
163 {
164     if (sw->ml) {
165         mac_learning_run(sw->ml, NULL);
166     }
167 }
168
169 void
170 lswitch_wait(struct lswitch *sw)
171 {
172     if (sw->ml) {
173         mac_learning_wait(sw->ml);
174     }
175 }
176
177 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
178  * to the learning switch state in 'sw'.  The most likely result of processing
179  * is that flow-setup and packet-out OpenFlow messages will be sent out on
180  * 'rconn'.  */
181 void
182 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
183                        const struct ofpbuf *msg)
184 {
185     const struct ofp_header *oh = msg->data;
186     const struct ofputil_msg_type *type;
187
188     if (sw->datapath_id == 0
189         && oh->type != OFPT_ECHO_REQUEST
190         && oh->type != OFPT_FEATURES_REPLY) {
191         send_features_request(sw, rconn);
192         return;
193     }
194
195     ofputil_decode_msg_type(oh, &type);
196     switch (ofputil_msg_type_code(type)) {
197     case OFPUTIL_OFPT_ECHO_REQUEST:
198         process_echo_request(sw, rconn, msg->data);
199         break;
200
201     case OFPUTIL_OFPT_FEATURES_REPLY:
202         process_switch_features(sw, msg->data);
203         break;
204
205     case OFPUTIL_OFPT_PACKET_IN:
206         process_packet_in(sw, rconn, msg->data);
207         break;
208
209     case OFPUTIL_OFPT_FLOW_REMOVED:
210         /* Nothing to do. */
211         break;
212
213     case OFPUTIL_INVALID:
214     case OFPUTIL_OFPT_HELLO:
215     case OFPUTIL_OFPT_ERROR:
216     case OFPUTIL_OFPT_ECHO_REPLY:
217     case OFPUTIL_OFPT_FEATURES_REQUEST:
218     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
219     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
220     case OFPUTIL_OFPT_SET_CONFIG:
221     case OFPUTIL_OFPT_PORT_STATUS:
222     case OFPUTIL_OFPT_PACKET_OUT:
223     case OFPUTIL_OFPT_FLOW_MOD:
224     case OFPUTIL_OFPT_PORT_MOD:
225     case OFPUTIL_OFPT_BARRIER_REQUEST:
226     case OFPUTIL_OFPT_BARRIER_REPLY:
227     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
228     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
229     case OFPUTIL_OFPST_DESC_REQUEST:
230     case OFPUTIL_OFPST_FLOW_REQUEST:
231     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
232     case OFPUTIL_OFPST_TABLE_REQUEST:
233     case OFPUTIL_OFPST_PORT_REQUEST:
234     case OFPUTIL_OFPST_QUEUE_REQUEST:
235     case OFPUTIL_OFPST_DESC_REPLY:
236     case OFPUTIL_OFPST_FLOW_REPLY:
237     case OFPUTIL_OFPST_QUEUE_REPLY:
238     case OFPUTIL_OFPST_PORT_REPLY:
239     case OFPUTIL_OFPST_TABLE_REPLY:
240     case OFPUTIL_OFPST_AGGREGATE_REPLY:
241     case OFPUTIL_NXT_ROLE_REQUEST:
242     case OFPUTIL_NXT_ROLE_REPLY:
243     case OFPUTIL_NXT_SET_FLOW_FORMAT:
244     case OFPUTIL_NXT_FLOW_MOD:
245     case OFPUTIL_NXT_FLOW_REMOVED:
246     case OFPUTIL_NXST_FLOW_REQUEST:
247     case OFPUTIL_NXST_AGGREGATE_REQUEST:
248     case OFPUTIL_NXST_FLOW_REPLY:
249     case OFPUTIL_NXST_AGGREGATE_REPLY:
250     default:
251         if (VLOG_IS_DBG_ENABLED()) {
252             char *s = ofp_to_string(msg->data, msg->size, 2);
253             VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
254                         sw->datapath_id, s);
255             free(s);
256         }
257     }
258 }
259 \f
260 static void
261 send_features_request(struct lswitch *sw, struct rconn *rconn)
262 {
263     time_t now = time_now();
264     if (now >= sw->last_features_request + 1) {
265         struct ofpbuf *b;
266         struct ofp_switch_config *osc;
267
268         /* Send OFPT_FEATURES_REQUEST. */
269         make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
270         queue_tx(sw, rconn, b);
271
272         /* Send OFPT_SET_CONFIG. */
273         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
274         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
275         queue_tx(sw, rconn, b);
276
277         sw->last_features_request = now;
278     }
279 }
280
281 static void
282 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
283 {
284     int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
285     if (retval && retval != ENOTCONN) {
286         if (retval == EAGAIN) {
287             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
288                          sw->datapath_id, rconn_get_name(rconn));
289         } else {
290             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
291                          sw->datapath_id, rconn_get_name(rconn),
292                          strerror(retval));
293         }
294     }
295 }
296
297 static void
298 process_switch_features(struct lswitch *sw, struct ofp_switch_features *osf)
299 {
300     size_t n_ports;
301     size_t i;
302
303     sw->datapath_id = ntohll(osf->datapath_id);
304
305     n_ports = (ntohs(osf->header.length) - sizeof *osf) / sizeof *osf->ports;
306     for (i = 0; i < n_ports; i++) {
307         struct ofp_phy_port *opp = &osf->ports[i];
308         struct lswitch_port *lp;
309
310         opp->name[OFP_MAX_PORT_NAME_LEN - 1] = '\0';
311         lp = shash_find_data(&sw->queue_names, opp->name);
312         if (lp && hmap_node_is_null(&lp->hmap_node)) {
313             lp->port_no = ntohs(opp->port_no);
314             hmap_insert(&sw->queue_numbers, &lp->hmap_node,
315                         hash_int(lp->port_no, 0));
316         }
317     }
318 }
319
320 static uint16_t
321 lswitch_choose_destination(struct lswitch *sw, const struct flow *flow)
322 {
323     uint16_t out_port;
324
325     /* Learn the source MAC. */
326     if (mac_learning_may_learn(sw->ml, flow->dl_src, 0)) {
327         struct mac_entry *mac = mac_learning_insert(sw->ml, flow->dl_src, 0);
328         if (mac_entry_is_new(mac) || mac->port.i != flow->in_port) {
329             VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
330                         "port %"PRIu16, sw->datapath_id,
331                         ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
332
333             mac->port.i = flow->in_port;
334             mac_learning_changed(sw->ml, mac);
335         }
336     }
337
338     /* Drop frames for reserved multicast addresses. */
339     if (eth_addr_is_reserved(flow->dl_dst)) {
340         return OFPP_NONE;
341     }
342
343     out_port = OFPP_FLOOD;
344     if (sw->ml) {
345         struct mac_entry *mac;
346
347         mac = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
348         if (mac) {
349             out_port = mac->port.i;
350             if (out_port == flow->in_port) {
351                 /* Don't send a packet back out its input port. */
352                 return OFPP_NONE;
353             }
354         }
355     }
356
357     /* Check if we need to use "NORMAL" action. */
358     if (sw->action_normal && out_port != OFPP_FLOOD) {
359         return OFPP_NORMAL;
360     }
361
362     return out_port;
363 }
364
365 static uint32_t
366 get_queue_id(const struct lswitch *sw, uint16_t in_port)
367 {
368     const struct lswitch_port *port;
369
370     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_int(in_port, 0),
371                              &sw->queue_numbers) {
372         if (port->port_no == in_port) {
373             return port->queue_id;
374         }
375     }
376
377     return sw->default_queue;
378 }
379
380 static void
381 process_packet_in(struct lswitch *sw, struct rconn *rconn,
382                   const struct ofp_packet_in *opi)
383 {
384     uint16_t in_port = ntohs(opi->in_port);
385     uint32_t queue_id;
386     uint16_t out_port;
387
388     struct ofp_action_header actions[2];
389     size_t actions_len;
390
391     size_t pkt_ofs, pkt_len;
392     struct ofpbuf pkt;
393     struct flow flow;
394
395     /* Ignore packets sent via output to OFPP_CONTROLLER.  This library never
396      * uses such an action.  You never know what experiments might be going on,
397      * though, and it seems best not to interfere with them. */
398     if (opi->reason != OFPR_NO_MATCH) {
399         return;
400     }
401
402     /* Extract flow data from 'opi' into 'flow'. */
403     pkt_ofs = offsetof(struct ofp_packet_in, data);
404     pkt_len = ntohs(opi->header.length) - pkt_ofs;
405     ofpbuf_use_const(&pkt, opi->data, pkt_len);
406     flow_extract(&pkt, 0, in_port, &flow);
407
408     /* Choose output port. */
409     out_port = lswitch_choose_destination(sw, &flow);
410
411     /* Make actions. */
412     queue_id = get_queue_id(sw, in_port);
413     if (out_port == OFPP_NONE) {
414         actions_len = 0;
415     } else if (queue_id == UINT32_MAX || out_port >= OFPP_MAX) {
416         struct ofp_action_output oao;
417
418         memset(&oao, 0, sizeof oao);
419         oao.type = htons(OFPAT_OUTPUT);
420         oao.len = htons(sizeof oao);
421         oao.port = htons(out_port);
422
423         memcpy(actions, &oao, sizeof oao);
424         actions_len = sizeof oao;
425     } else {
426         struct ofp_action_enqueue oae;
427
428         memset(&oae, 0, sizeof oae);
429         oae.type = htons(OFPAT_ENQUEUE);
430         oae.len = htons(sizeof oae);
431         oae.port = htons(out_port);
432         oae.queue_id = htonl(queue_id);
433
434         memcpy(actions, &oae, sizeof oae);
435         actions_len = sizeof oae;
436     }
437     assert(actions_len <= sizeof actions);
438
439     /* Send the packet, and possibly the whole flow, to the output port. */
440     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
441         struct ofpbuf *buffer;
442         struct cls_rule rule;
443
444         /* The output port is known, or we always flood everything, so add a
445          * new flow. */
446         cls_rule_init(&flow, &sw->wc, 0, &rule);
447         buffer = make_add_flow(&rule, ntohl(opi->buffer_id),
448                                sw->max_idle, actions_len);
449         ofpbuf_put(buffer, actions, actions_len);
450         queue_tx(sw, rconn, buffer);
451
452         /* If the switch didn't buffer the packet, we need to send a copy. */
453         if (ntohl(opi->buffer_id) == UINT32_MAX && actions_len > 0) {
454             queue_tx(sw, rconn,
455                      make_packet_out(&pkt, UINT32_MAX, in_port,
456                                      actions, actions_len / sizeof *actions));
457         }
458     } else {
459         /* We don't know that MAC, or we don't set up flows.  Send along the
460          * packet without setting up a flow. */
461         if (ntohl(opi->buffer_id) != UINT32_MAX || actions_len > 0) {
462             queue_tx(sw, rconn,
463                      make_packet_out(&pkt, ntohl(opi->buffer_id), in_port,
464                                      actions, actions_len / sizeof *actions));
465         }
466     }
467 }
468
469 static void
470 process_echo_request(struct lswitch *sw, struct rconn *rconn,
471                      const struct ofp_header *rq)
472 {
473     queue_tx(sw, rconn, make_echo_reply(rq));
474 }