1 /* Copyright (c) 2015 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include "byte-order.h"
22 #include "openflow/openflow.h"
23 #include "openvswitch/dynamic-string.h"
24 #include "openvswitch/match.h"
25 #include "openvswitch/ofp-actions.h"
26 #include "openvswitch/ofp-msgs.h"
27 #include "openvswitch/ofp-print.h"
28 #include "openvswitch/ofp-util.h"
29 #include "openvswitch/ofpbuf.h"
30 #include "openvswitch/vlog.h"
31 #include "ovn-controller.h"
34 #include "socket-util.h"
36 #include "vswitch-idl.h"
38 VLOG_DEFINE_THIS_MODULE(ofctrl);
40 /* An OpenFlow flow. */
43 struct hmap_node hmap_node;
49 struct ofpact *ofpacts;
53 static uint32_t ovn_flow_hash(const struct ovn_flow *);
54 static struct ovn_flow *ovn_flow_lookup(struct hmap *flow_table,
55 const struct ovn_flow *target);
56 static char *ovn_flow_to_string(const struct ovn_flow *);
57 static void ovn_flow_log(const struct ovn_flow *, const char *action);
58 static void ovn_flow_destroy(struct ovn_flow *);
60 static ovs_be32 queue_msg(struct ofpbuf *);
61 static void queue_flow_mod(struct ofputil_flow_mod *);
63 /* OpenFlow connection to the switch. */
64 static struct rconn *swconn;
66 /* Last seen sequence number for 'swconn'. When this differs from
67 * rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
68 static unsigned int seqno;
70 /* Connection state machine. */
73 STATE(S_TLV_TABLE_REQUESTED) \
74 STATE(S_TLV_TABLE_MOD_SENT) \
75 STATE(S_CLEAR_FLOWS) \
78 #define STATE(NAME) NAME,
84 static enum ofctrl_state state;
86 /* Transaction IDs for messages in flight to the switch. */
87 static ovs_be32 xid, xid2;
89 /* Counter for in-flight OpenFlow messages on 'swconn'. We only send a new
90 * round of flow table modifications to the switch when the counter falls to
91 * zero, to avoid unbounded buffering. */
92 static struct rconn_packet_counter *tx_counter;
94 /* Flow table of "struct ovn_flow"s, that holds the flow table currently
95 * installed in the switch. */
96 static struct hmap installed_flows;
98 /* MFF_* field ID for our Geneve option. In S_TLV_TABLE_MOD_SENT, this is
99 * the option we requested (we don't know whether we obtained it yet). In
100 * S_CLEAR_FLOWS or S_UPDATE_FLOWS, this is really the option we have. */
101 static enum mf_field_id mff_ovn_geneve;
103 static void ovn_flow_table_clear(struct hmap *flow_table);
104 static void ovn_flow_table_destroy(struct hmap *flow_table);
106 static void ofctrl_recv(const struct ofp_header *, enum ofptype);
111 swconn = rconn_create(5, 0, DSCP_DEFAULT, 1 << OFP13_VERSION);
112 tx_counter = rconn_packet_counter_create();
113 hmap_init(&installed_flows);
116 /* S_NEW, for a new connection.
118 * Sends NXT_TLV_TABLE_REQUEST and transitions to
119 * S_TLV_TABLE_REQUESTED. */
124 struct ofpbuf *buf = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_REQUEST,
125 rconn_get_version(swconn), 0);
126 xid = queue_msg(buf);
127 state = S_TLV_TABLE_REQUESTED;
131 recv_S_NEW(const struct ofp_header *oh OVS_UNUSED,
132 enum ofptype type OVS_UNUSED)
137 /* S_TLV_TABLE_REQUESTED, when NXT_TLV_TABLE_REQUEST has been sent
138 * and we're waiting for a reply.
140 * If we receive an NXT_TLV_TABLE_REPLY:
142 * - If it contains our tunnel metadata option, assign its field ID to
143 * mff_ovn_geneve and transition to S_CLEAR_FLOWS.
145 * - Otherwise, if there is an unused tunnel metadata field ID, send
146 * NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST, and transition to
147 * S_TLV_TABLE_MOD_SENT.
149 * - Otherwise, log an error, disable Geneve, and transition to
152 * If we receive an OFPT_ERROR:
154 * - Log an error, disable Geneve, and transition to S_CLEAR_FLOWS. */
157 run_S_TLV_TABLE_REQUESTED(void)
162 recv_S_TLV_TABLE_REQUESTED(const struct ofp_header *oh, enum ofptype type)
164 if (oh->xid != xid) {
165 ofctrl_recv(oh, type);
166 } else if (type == OFPTYPE_NXT_TLV_TABLE_REPLY) {
167 struct ofputil_tlv_table_reply reply;
168 enum ofperr error = ofputil_decode_tlv_table_reply(oh, &reply);
170 VLOG_ERR("failed to decode TLV table request (%s)",
171 ofperr_to_string(error));
175 const struct ofputil_tlv_map *map;
176 uint64_t md_free = UINT64_MAX;
177 BUILD_ASSERT(TUN_METADATA_NUM_OPTS == 64);
179 LIST_FOR_EACH (map, list_node, &reply.mappings) {
180 if (map->option_class == OVN_GENEVE_CLASS
181 && map->option_type == OVN_GENEVE_TYPE
182 && map->option_len == OVN_GENEVE_LEN) {
183 if (map->index >= TUN_METADATA_NUM_OPTS) {
184 VLOG_ERR("desired Geneve tunnel option 0x%"PRIx16","
185 "%"PRIu8",%"PRIu8" already in use with "
186 "unsupported index %"PRIu16,
187 map->option_class, map->option_type,
188 map->option_len, map->index);
191 mff_ovn_geneve = MFF_TUN_METADATA0 + map->index;
192 state = S_CLEAR_FLOWS;
197 if (map->index < TUN_METADATA_NUM_OPTS) {
198 md_free &= ~(UINT64_C(1) << map->index);
202 VLOG_DBG("OVN Geneve option not found");
204 VLOG_ERR("no Geneve options free for use by OVN");
208 unsigned int index = rightmost_1bit_idx(md_free);
209 mff_ovn_geneve = MFF_TUN_METADATA0 + index;
210 struct ofputil_tlv_map tm;
211 tm.option_class = OVN_GENEVE_CLASS;
212 tm.option_type = OVN_GENEVE_TYPE;
213 tm.option_len = OVN_GENEVE_LEN;
216 struct ofputil_tlv_table_mod ttm;
217 ttm.command = NXTTMC_ADD;
218 ovs_list_init(&ttm.mappings);
219 ovs_list_push_back(&ttm.mappings, &tm.list_node);
221 xid = queue_msg(ofputil_encode_tlv_table_mod(OFP13_VERSION, &ttm));
222 xid2 = queue_msg(ofputil_encode_barrier_request(OFP13_VERSION));
223 state = S_TLV_TABLE_MOD_SENT;
224 } else if (type == OFPTYPE_ERROR) {
225 VLOG_ERR("switch refused to allocate Geneve option (%s)",
226 ofperr_to_string(ofperr_decode_msg(oh, NULL)));
229 char *s = ofp_to_string(oh, ntohs(oh->length), 1);
230 VLOG_ERR("unexpected reply to TLV table request (%s)",
239 state = S_CLEAR_FLOWS;
242 /* S_TLV_TABLE_MOD_SENT, when NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST
243 * have been sent and we're waiting for a reply to one or the other.
245 * If we receive an OFPT_ERROR:
247 * - If the error is NXTTMFC_ALREADY_MAPPED or NXTTMFC_DUP_ENTRY, we
248 * raced with some other controller. Transition to S_NEW.
250 * - Otherwise, log an error, disable Geneve, and transition to
253 * If we receive OFPT_BARRIER_REPLY:
255 * - Set the tunnel metadata field ID to the one that we requested.
256 * Transition to S_CLEAR_FLOWS.
260 run_S_TLV_TABLE_MOD_SENT(void)
265 recv_S_TLV_TABLE_MOD_SENT(const struct ofp_header *oh, enum ofptype type)
267 if (oh->xid != xid && oh->xid != xid2) {
268 ofctrl_recv(oh, type);
269 } else if (oh->xid == xid2 && type == OFPTYPE_BARRIER_REPLY) {
270 state = S_CLEAR_FLOWS;
271 } else if (oh->xid == xid && type == OFPTYPE_ERROR) {
272 enum ofperr error = ofperr_decode_msg(oh, NULL);
273 if (error == OFPERR_NXTTMFC_ALREADY_MAPPED ||
274 error == OFPERR_NXTTMFC_DUP_ENTRY) {
275 VLOG_INFO("raced with another controller adding "
276 "Geneve option (%s); trying again",
277 ofperr_to_string(error));
280 VLOG_ERR("error adding Geneve option (%s)",
281 ofperr_to_string(error));
285 char *s = ofp_to_string(oh, ntohs(oh->length), 1);
286 VLOG_ERR("unexpected reply to Geneve option allocation request (%s)",
294 state = S_CLEAR_FLOWS;
297 /* S_CLEAR_FLOWS, after we've established a Geneve metadata field ID and it's
298 * time to set up some flows.
300 * Sends an OFPT_TABLE_MOD to clear all flows, then transitions to
304 run_S_CLEAR_FLOWS(void)
306 /* Send a flow_mod to delete all flows. */
307 struct ofputil_flow_mod fm = {
308 .match = MATCH_CATCHALL_INITIALIZER,
309 .table_id = OFPTT_ALL,
310 .command = OFPFC_DELETE,
313 VLOG_DBG("clearing all flows");
315 /* Clear installed_flows, to match the state of the switch. */
316 ovn_flow_table_clear(&installed_flows);
318 state = S_UPDATE_FLOWS;
322 recv_S_CLEAR_FLOWS(const struct ofp_header *oh, enum ofptype type)
324 ofctrl_recv(oh, type);
327 /* S_UPDATE_FLOWS, for maintaining the flow table over time.
329 * Compare the installed flows to the ones we want. Send OFPT_FLOW_MOD as
332 * This is a terminal state. We only transition out of it if the connection
336 run_S_UPDATE_FLOWS(void)
338 /* Nothing to do here.
340 * Being in this state enables ofctrl_put() to work, however. */
344 recv_S_UPDATE_FLOWS(const struct ofp_header *oh, enum ofptype type)
346 ofctrl_recv(oh, type);
349 /* Runs the OpenFlow state machine against 'br_int', which is local to the
350 * hypervisor on which we are running. Attempts to negotiate a Geneve option
351 * field for class OVN_GENEVE_CLASS, type OVN_GENEVE_TYPE. If successful,
352 * returns the MFF_* field ID for the option, otherwise returns 0. */
354 ofctrl_run(const struct ovsrec_bridge *br_int)
358 target = xasprintf("unix:%s/%s.mgmt", ovs_rundir(), br_int->name);
359 if (strcmp(target, rconn_get_target(swconn))) {
360 VLOG_INFO("%s: connecting to switch", target);
361 rconn_connect(swconn, target, target);
365 rconn_disconnect(swconn);
370 if (!rconn_is_connected(swconn)) {
373 if (seqno != rconn_get_connection_seqno(swconn)) {
374 seqno = rconn_get_connection_seqno(swconn);
378 enum ofctrl_state old_state;
382 #define STATE(NAME) case NAME: run_##NAME(); break;
388 } while (state != old_state);
390 for (int i = 0; state == old_state && i < 50; i++) {
391 struct ofpbuf *msg = rconn_recv(swconn);
396 const struct ofp_header *oh = msg->data;
400 error = ofptype_decode(&type, oh);
403 #define STATE(NAME) case NAME: recv_##NAME(oh, type); break;
410 char *s = ofp_to_string(oh, ntohs(oh->length), 1);
411 VLOG_WARN("could not decode OpenFlow message (%s): %s",
412 ofperr_to_string(error), s);
419 return (state == S_CLEAR_FLOWS || state == S_UPDATE_FLOWS
420 ? mff_ovn_geneve : 0);
426 rconn_run_wait(swconn);
427 rconn_recv_wait(swconn);
433 rconn_destroy(swconn);
434 ovn_flow_table_destroy(&installed_flows);
435 rconn_packet_counter_destroy(tx_counter);
439 queue_msg(struct ofpbuf *msg)
441 const struct ofp_header *oh = msg->data;
442 ovs_be32 xid = oh->xid;
443 rconn_send(swconn, msg, tx_counter);
448 ofctrl_recv(const struct ofp_header *oh, enum ofptype type)
450 if (type == OFPTYPE_ECHO_REQUEST) {
451 queue_msg(make_echo_reply(oh));
452 } else if (type != OFPTYPE_ECHO_REPLY &&
453 type != OFPTYPE_BARRIER_REPLY &&
454 type != OFPTYPE_PACKET_IN &&
455 type != OFPTYPE_PORT_STATUS &&
456 type != OFPTYPE_FLOW_REMOVED) {
457 if (VLOG_IS_DBG_ENABLED()) {
458 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
460 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
461 VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", s);
467 /* Flow table interface to the rest of ovn-controller. */
469 /* Adds a flow to 'desired_flows' with the specified 'match' and 'actions' to
470 * the OpenFlow table numbered 'table_id' with the given 'priority'. The
471 * caller retains ownership of 'match' and 'actions'.
473 * This just assembles the desired flow table in memory. Nothing is actually
474 * sent to the switch until a later call to ofctrl_run().
476 * The caller should initialize its own hmap to hold the flows. */
478 ofctrl_add_flow(struct hmap *desired_flows,
479 uint8_t table_id, uint16_t priority,
480 const struct match *match, const struct ofpbuf *actions)
482 struct ovn_flow *f = xmalloc(sizeof *f);
483 f->table_id = table_id;
484 f->priority = priority;
486 f->ofpacts = xmemdup(actions->data, actions->size);
487 f->ofpacts_len = actions->size;
488 f->hmap_node.hash = ovn_flow_hash(f);
490 if (ovn_flow_lookup(desired_flows, f)) {
491 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
492 if (!VLOG_DROP_INFO(&rl)) {
493 char *s = ovn_flow_to_string(f);
494 VLOG_INFO("dropping duplicate flow: %s", s);
502 hmap_insert(desired_flows, &f->hmap_node, f->hmap_node.hash);
507 /* Returns a hash of the key in 'f'. */
509 ovn_flow_hash(const struct ovn_flow *f)
511 return hash_2words((f->table_id << 16) | f->priority,
512 match_hash(&f->match, 0));
516 /* Finds and returns an ovn_flow in 'flow_table' whose key is identical to
517 * 'target''s key, or NULL if there is none. */
518 static struct ovn_flow *
519 ovn_flow_lookup(struct hmap *flow_table, const struct ovn_flow *target)
523 HMAP_FOR_EACH_WITH_HASH (f, hmap_node, target->hmap_node.hash,
525 if (f->table_id == target->table_id
526 && f->priority == target->priority
527 && match_equal(&f->match, &target->match)) {
535 ovn_flow_to_string(const struct ovn_flow *f)
537 struct ds s = DS_EMPTY_INITIALIZER;
538 ds_put_format(&s, "table_id=%"PRIu8", ", f->table_id);
539 ds_put_format(&s, "priority=%"PRIu16", ", f->priority);
540 match_format(&f->match, &s, OFP_DEFAULT_PRIORITY);
541 ds_put_cstr(&s, ", actions=");
542 ofpacts_format(f->ofpacts, f->ofpacts_len, &s);
543 return ds_steal_cstr(&s);
547 ovn_flow_log(const struct ovn_flow *f, const char *action)
549 if (VLOG_IS_DBG_ENABLED()) {
550 char *s = ovn_flow_to_string(f);
551 VLOG_DBG("%s flow: %s", action, s);
557 ovn_flow_destroy(struct ovn_flow *f)
565 /* Flow tables of struct ovn_flow. */
568 ovn_flow_table_clear(struct hmap *flow_table)
570 struct ovn_flow *f, *next;
571 HMAP_FOR_EACH_SAFE (f, next, hmap_node, flow_table) {
572 hmap_remove(flow_table, &f->hmap_node);
578 ovn_flow_table_destroy(struct hmap *flow_table)
580 ovn_flow_table_clear(flow_table);
581 hmap_destroy(flow_table);
584 /* Flow table update. */
587 queue_flow_mod(struct ofputil_flow_mod *fm)
589 fm->buffer_id = UINT32_MAX;
590 fm->out_port = OFPP_ANY;
591 fm->out_group = OFPG_ANY;
592 queue_msg(ofputil_encode_flow_mod(fm, OFPUTIL_P_OF13_OXM));
595 /* Replaces the flow table on the switch, if possible, by the flows in
596 * 'flow_table', which should have been added with ofctrl_add_flow().
597 * Regardless of whether the flow table is updated, this deletes all of the
598 * flows from 'flow_table' and frees them. (The hmap itself isn't
601 * This called be called be ofctrl_run() within the main loop. */
603 ofctrl_put(struct hmap *flow_table)
605 /* The flow table can be updated if the connection to the switch is up and
606 * in the correct state and not backlogged with existing flow_mods. (Our
607 * criteria for being backlogged appear very conservative, but the socket
608 * between ovn-controller and OVS provides some buffering.) Otherwise,
609 * discard the flows. A solution to either of those problems will cause us
610 * to wake up and retry. */
611 if (state != S_UPDATE_FLOWS
612 || rconn_packet_counter_n_packets(tx_counter)) {
613 ovn_flow_table_clear(flow_table);
617 /* Iterate through all of the installed flows. If any of them are no
618 * longer desired, delete them; if any of them should have different
619 * actions, update them. */
620 struct ovn_flow *i, *next;
621 HMAP_FOR_EACH_SAFE (i, next, hmap_node, &installed_flows) {
622 struct ovn_flow *d = ovn_flow_lookup(flow_table, i);
624 /* Installed flow is no longer desirable. Delete it from the
625 * switch and from installed_flows. */
626 struct ofputil_flow_mod fm = {
628 .priority = i->priority,
629 .table_id = i->table_id,
630 .command = OFPFC_DELETE_STRICT,
633 ovn_flow_log(i, "removing");
635 hmap_remove(&installed_flows, &i->hmap_node);
638 if (!ofpacts_equal(i->ofpacts, i->ofpacts_len,
639 d->ofpacts, d->ofpacts_len)) {
640 /* Update actions in installed flow. */
641 struct ofputil_flow_mod fm = {
643 .priority = i->priority,
644 .table_id = i->table_id,
645 .ofpacts = d->ofpacts,
646 .ofpacts_len = d->ofpacts_len,
647 .command = OFPFC_MODIFY_STRICT,
650 ovn_flow_log(i, "updating");
652 /* Replace 'i''s actions by 'd''s. */
654 i->ofpacts = d->ofpacts;
655 i->ofpacts_len = d->ofpacts_len;
660 hmap_remove(flow_table, &d->hmap_node);
665 /* The previous loop removed from 'flow_table' all of the flows that are
666 * already installed. Thus, any flows remaining in 'flow_table' need to
667 * be added to the flow table. */
669 HMAP_FOR_EACH_SAFE (d, next, hmap_node, flow_table) {
670 /* Send flow_mod to add flow. */
671 struct ofputil_flow_mod fm = {
673 .priority = d->priority,
674 .table_id = d->table_id,
675 .ofpacts = d->ofpacts,
676 .ofpacts_len = d->ofpacts_len,
677 .command = OFPFC_ADD,
680 ovn_flow_log(d, "adding");
682 /* Move 'd' from 'flow_table' to installed_flows. */
683 hmap_remove(flow_table, &d->hmap_node);
684 hmap_insert(&installed_flows, &d->hmap_node, d->hmap_node.hash);