From patchwork Tue Jun 7 05:49:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Numan Siddique X-Patchwork-Id: 631320 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (archives.nicira.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 3rP0y000J8z9s9Z for ; Tue, 7 Jun 2016 15:49:27 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 94DF310688; Mon, 6 Jun 2016 22:49:26 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx1e4.cudamail.com (mx1.cudamail.com [69.90.118.67]) by archives.nicira.com (Postfix) with ESMTPS id 7CF1710136 for ; Mon, 6 Jun 2016 22:49:25 -0700 (PDT) Received: from bar5.cudamail.com (unknown [192.168.21.12]) by mx1e4.cudamail.com (Postfix) with ESMTPS id 0C2B11E006F for ; Mon, 6 Jun 2016 23:49:25 -0600 (MDT) X-ASG-Debug-ID: 1465278564-09eadd423eb0ca0001-byXFYA Received: from mx1-pf1.cudamail.com ([192.168.24.1]) by bar5.cudamail.com with ESMTP id cWzA2zrm3kwmRDfy (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Mon, 06 Jun 2016 23:49:24 -0600 (MDT) X-Barracuda-Envelope-From: nusiddiq@redhat.com X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.1 Received: from unknown (HELO mx1.redhat.com) (209.132.183.28) by mx1-pf1.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 7 Jun 2016 05:49:23 -0000 Received-SPF: pass (mx1-pf1.cudamail.com: SPF record at _spf1.redhat.com designates 209.132.183.28 as permitted sender) X-Barracuda-Apparent-Source-IP: 209.132.183.28 X-Barracuda-RBL-IP: 209.132.183.28 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A9FA312B25 for ; Tue, 7 Jun 2016 05:49:22 +0000 (UTC) Received: from nusiddiq.blr.redhat.com (ovpn-116-51.phx2.redhat.com [10.3.116.51]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u575nK9M018752 for ; Tue, 7 Jun 2016 01:49:21 -0400 X-CudaMail-Envelope-Sender: nusiddiq@redhat.com From: Numan Siddique X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E1-605094540 X-CudaMail-DTE: 060616 X-CudaMail-Originating-IP: 209.132.183.28 To: ovs dev X-ASG-Orig-Subj: [##CM-E1-605094540##][PATCH v10 1/2] ovn-controller: Add 'put_dhcp_opts' action in ovn-controller In-Reply-To: <9780b758-1843-f08a-5f5a-1a63331c1035@redhat.com> Organization: Red Hat Message-ID: <946c6e61-0ce2-30ce-fe4a-25946e8f84f8@redhat.com> Date: Tue, 7 Jun 2016 11:19:19 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 07 Jun 2016 05:49:22 +0000 (UTC) X-Barracuda-Connect: UNKNOWN[192.168.24.1] X-Barracuda-Start-Time: 1465278564 X-Barracuda-Encrypted: ECDHE-RSA-AES256-GCM-SHA384 X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Barracuda-BRTS-Status: 1 X-Virus-Scanned: by bsmtpd at cudamail.com Subject: [ovs-dev] [PATCH v10 1/2] ovn-controller: Add 'put_dhcp_opts' action in ovn-controller X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@openvswitch.org Sender: "dev" This patch adds a new OVN action 'put_dhcp_opts' to support native DHCP in OVN. ovn-controller parses this action and adds a NXT_PACKET_IN2 OF flow with 'pause' flag set and the DHCP options stored in 'userdata' field. When the valid DHCP packet is received by ovn-controller, it frames a new DHCP reply packet with the DHCP options present in the 'userdata' field and resumes the packet and stores 1 in the OVS register provded in the 'put_dhcp_opts' parameter. If the packet is invalid, it resumes the packet without any modifying and stores 0 in the OVS register. Eg. put_dhcp_opts(reg0, efferip = 10.0.0.4, router = 10.0.0.1, netmask = 255.255.255.0, lease_time = 3600,....) A new 'DHCP_Options' table is added in SB DB which stores the supported DHCP options with DHCP code and type. ovn-northd is expected to popule this table. The next patch will add logical flows with this action. Signed-Off-by: Numan Siddique --- include/openvswitch/meta-flow.h | 12 +++ lib/dhcp.h | 13 +++ ovn/controller/lflow.c | 11 +++ ovn/controller/pinctrl.c | 188 ++++++++++++++++++++++++++++++++++++- ovn/lib/actions.c | 203 +++++++++++++++++++++++++++++++++++++++- ovn/lib/actions.h | 8 ++ ovn/lib/automake.mk | 1 + ovn/lib/expr.c | 48 +++------- ovn/lib/expr.h | 37 ++++++++ ovn/lib/ovn-dhcp.h | 111 ++++++++++++++++++++++ ovn/ovn-sb.ovsschema | 16 +++- ovn/ovn-sb.xml | 193 ++++++++++++++++++++++++++++++++++++++ tests/automake.mk | 1 + tests/ovn.at | 51 ++++++++++ tests/test-ovn-dhcp.c | 149 +++++++++++++++++++++++++++++ 15 files changed, 1001 insertions(+), 41 deletions(-) create mode 100644 ovn/lib/ovn-dhcp.h create mode 100644 tests/test-ovn-dhcp.c diff --git a/include/openvswitch/meta-flow.h b/include/openvswitch/meta-flow.h index e5f9962..84a0946 100644 --- a/include/openvswitch/meta-flow.h +++ b/include/openvswitch/meta-flow.h @@ -1930,6 +1930,18 @@ union mf_subvalue { /* Convenient access to just least-significant bits in various forms. */ struct { + uint8_t dummy_u8[127]; + uint8_t u8_val; + }; + struct { + ovs_be16 dummy_be16[63]; + ovs_be16 be16_int; + }; + struct { + ovs_be32 dummy_be32[31]; + ovs_be32 be32_int; + }; + struct { ovs_be64 dummy_integer[15]; ovs_be64 integer; }; diff --git a/lib/dhcp.h b/lib/dhcp.h index 6f97298..271e0a5 100644 --- a/lib/dhcp.h +++ b/lib/dhcp.h @@ -25,6 +25,8 @@ #define DHCP_SERVER_PORT 67 /* Port used by DHCP server. */ #define DHCP_CLIENT_PORT 68 /* Port used by DHCP client. */ +#define DHCP_MAGIC_COOKIE 0x63825363 + #define DHCP_HEADER_LEN 236 struct dhcp_header { uint8_t op; /* DHCP_BOOTREQUEST or DHCP_BOOTREPLY. */ @@ -45,4 +47,15 @@ struct dhcp_header { }; BUILD_ASSERT_DECL(DHCP_HEADER_LEN == sizeof(struct dhcp_header)); +#define DHCP_OP_REQUEST 1 +#define DHCP_OP_REPLY 2 + +#define DHCP_MSG_DISCOVER 1 +#define DHCP_MSG_OFFER 2 +#define DHCP_MSG_REQUEST 3 +#define DHCP_MSG_ACK 5 + +#define DHCP_OPT_MSG_TYPE 53 +#define DHCP_OPT_END 255 + #endif /* dhcp.h */ diff --git a/ovn/controller/lflow.c b/ovn/controller/lflow.c index efc427d..52e6131 100644 --- a/ovn/controller/lflow.c +++ b/ovn/controller/lflow.c @@ -24,6 +24,7 @@ #include "ovn-controller.h" #include "ovn/lib/actions.h" #include "ovn/lib/expr.h" +#include "ovn/lib/ovn-dhcp.h" #include "ovn/lib/ovn-sb-idl.h" #include "packets.h" #include "simap.h" @@ -203,6 +204,13 @@ add_logical_flows(struct controller_ctx *ctx, const struct lport_index *lports, { uint32_t conj_id_ofs = 1; + struct hmap dhcp_opts = HMAP_INITIALIZER(&dhcp_opts); + const struct sbrec_dhcp_options *dhcp_opt_row; + SBREC_DHCP_OPTIONS_FOR_EACH(dhcp_opt_row, ctx->ovnsb_idl) { + dhcp_opt_add(&dhcp_opts, dhcp_opt_row->name, dhcp_opt_row->code, + dhcp_opt_row->type); + } + const struct sbrec_logical_flow *lflow; SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->ovnsb_idl) { /* Determine translation of logical table IDs to physical table IDs. */ @@ -274,6 +282,7 @@ add_logical_flows(struct controller_ctx *ctx, const struct lport_index *lports, }; struct action_params ap = { .symtab = &symtab, + .dhcp_opts = &dhcp_opts, .lookup_port = lookup_port_cb, .aux = &aux, .ct_zones = ct_zones, @@ -357,6 +366,8 @@ add_logical_flows(struct controller_ctx *ctx, const struct lport_index *lports, ofpbuf_uninit(&ofpacts); conj_id_ofs += n_conjs; } + + dhcp_opts_destroy(&dhcp_opts); } static void diff --git a/ovn/controller/pinctrl.c b/ovn/controller/pinctrl.c index bc57c40..244dd3d 100644 --- a/ovn/controller/pinctrl.c +++ b/ovn/controller/pinctrl.c @@ -18,6 +18,7 @@ #include "pinctrl.h" #include "coverage.h" +#include "csum.h" #include "dirs.h" #include "dp-packet.h" #include "flow.h" @@ -29,6 +30,8 @@ #include "openvswitch/ofp-print.h" #include "openvswitch/ofp-util.h" #include "openvswitch/vlog.h" + +#include "lib/dhcp.h" #include "ovn-controller.h" #include "ovn/lib/actions.h" #include "ovn/lib/logical-fields.h" @@ -204,13 +207,192 @@ exit: } static void +pinctrl_handle_put_dhcp_opts( + struct dp_packet *pkt_in, struct ofputil_packet_in *pin, + struct ofpbuf *userdata, struct ofpbuf *continuation) +{ + enum ofp_version version = rconn_get_version(swconn); + enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version); + struct dp_packet *pkt_out_ptr = NULL; + uint32_t success = 0; + + /* Validate the userdata. Format of the userdata is + * + * --------------------------------------------------- + * | REG IDX (4 B) | OFFER IP | DHCP options | + * | (to store the result) | (4 B) | | + * --------------------------------------------------- + */ + uint32_t *reg_idx = ofpbuf_try_pull(userdata, sizeof *reg_idx); + if (!reg_idx) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "reg_idx not present in the userdata"); + goto exit; + } + + ovs_be32 *offer_ip = ofpbuf_try_pull(userdata, sizeof *offer_ip); + if (!offer_ip) { + /* offer_ip is not present in userdata. Resume the packet without + * further processing. */ + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "offer_ip not present in the userdata"); + goto exit; + } + + if (!userdata->size) { + /* No DHCP options present in the 'userdata'. Resume the packet + * without further processing */ + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "DHCP options not present in the userdata"); + goto exit; + } + + /* Validate the DHCP request packet. + * Format of the DHCP packet is + * ------------------------------------------------------------------------ + *| UDP HEADER | DHCP HEADER | 4 Byte DHCP Cookie | DHCP OPTIONS(var len)| + * ------------------------------------------------------------------------ + */ + if (dp_packet_l4_size(pkt_in) < (UDP_HEADER_LEN + + sizeof (struct dhcp_header) + sizeof(uint32_t) + 3)) { + /* Invalid or incomplete DHCP packet. Resume the packet without + * further processing. */ + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "Invalid or incomplete DHCP packet recieved"); + goto exit; + } + + struct dhcp_header const *in_dhcp_data = dp_packet_get_udp_payload(pkt_in); + if (in_dhcp_data->op != DHCP_OP_REQUEST) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "Invalid opcode in the DHCP packet : %d", + in_dhcp_data->op); + goto exit; + } + + /* DHCP options follow the DHCP header. The first 4 bytes of the DHCP + * options is the DHCP magic cookie followed by the actual DHCP options. + */ + const uint8_t *in_dhcp_opt = + (const uint8_t *)dp_packet_get_udp_payload(pkt_in) + + sizeof (struct dhcp_header); + + ovs_be32 magic_cookie = htonl(DHCP_MAGIC_COOKIE); + if (memcmp(in_dhcp_opt, &magic_cookie, sizeof(ovs_be32))) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "DHCP magic cookie not present in the DHCP packet"); + goto exit; + } + + in_dhcp_opt += 4; + /* Check that the DHCP Message Type (opt 53) is present or not with + * valid values - DHCP_MSG_DISCOVER or DHCP_MSG_REQUEST as the first + * DHCP option. + */ + if (!(in_dhcp_opt[0] == DHCP_OPT_MSG_TYPE && in_dhcp_opt[1] == 1 && ( + in_dhcp_opt[2] == DHCP_MSG_DISCOVER || + in_dhcp_opt[2] == DHCP_MSG_REQUEST))) { + /* Invalid DHCP packet. Resume the packet without further + * processing. */ + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + VLOG_WARN_RL(&rl, "Invalid DHCP message type : opt code = %d," + " opt value = %d", in_dhcp_opt[0], in_dhcp_opt[2]); + goto exit; + } + + uint8_t msg_type; + if (in_dhcp_opt[2] == DHCP_MSG_DISCOVER) { + msg_type = DHCP_MSG_OFFER; + } else { + msg_type = DHCP_MSG_ACK; + } + + /* Frame the DHCP reply packet + * Total DHCP options length will be options stored in the userdata + + * 16 bytes. + * + * -------------------------------------------------------------- + *| 4 Bytes (dhcp cookie) | 3 Bytes (option type) | DHCP options | + * -------------------------------------------------------------- + *| 4 Bytes padding | 1 Byte (option end 0xFF ) | 4 Bytes padding| + * -------------------------------------------------------------- + */ + uint16_t new_l4_size = UDP_HEADER_LEN + DHCP_HEADER_LEN + \ + userdata->size + 16; + size_t new_packet_size = pkt_in->l4_ofs + new_l4_size; + + struct dp_packet pkt_out; + dp_packet_init(&pkt_out, new_packet_size); + dp_packet_clear(&pkt_out); + dp_packet_prealloc_tailroom(&pkt_out, new_packet_size); + pkt_out_ptr = &pkt_out; + + /* Copy the L2 and L3 headers from the pkt_in as they would remain same*/ + dp_packet_put( + &pkt_out, dp_packet_pull(pkt_in, pkt_in->l4_ofs), pkt_in->l4_ofs); + + pkt_out.l2_5_ofs = pkt_in->l2_5_ofs; + pkt_out.l2_pad_size = pkt_in->l2_pad_size; + pkt_out.l3_ofs = pkt_in->l3_ofs; + pkt_out.l4_ofs = pkt_in->l4_ofs; + + struct udp_header *udp = dp_packet_put( + &pkt_out, dp_packet_pull(pkt_in, UDP_HEADER_LEN), UDP_HEADER_LEN); + + struct dhcp_header *dhcp_data = dp_packet_put( + &pkt_out, dp_packet_pull(pkt_in, DHCP_HEADER_LEN), DHCP_HEADER_LEN); + dhcp_data->op = DHCP_OP_REPLY; + dhcp_data->yiaddr = *offer_ip; + dp_packet_put(&pkt_out, &magic_cookie, sizeof(ovs_be32)); + + uint8_t *out_dhcp_opts = dp_packet_put_zeros(&pkt_out, + userdata->size + 12); + /* DHCP option - type */ + out_dhcp_opts[0] = DHCP_OPT_MSG_TYPE; + out_dhcp_opts[1] = 1; + out_dhcp_opts[2] = msg_type; + out_dhcp_opts += 3; + + memcpy(out_dhcp_opts, userdata->data, userdata->size); + out_dhcp_opts += userdata->size; + /* Padding */ + out_dhcp_opts += 4; + /* End */ + out_dhcp_opts[0] = DHCP_OPT_END; + + udp->udp_len = htons(new_l4_size); + + struct ip_header *out_ip = dp_packet_l3(&pkt_out); + out_ip->ip_tot_len = htons(pkt_out.l4_ofs - pkt_out.l3_ofs + new_l4_size); + udp->udp_csum = 0; + out_ip->ip_csum = 0; + out_ip->ip_csum = csum(out_ip, sizeof *out_ip); + + pin->packet = dp_packet_data(&pkt_out); + pin->packet_len = dp_packet_size(&pkt_out); + + success = 1; +exit: + /* store the result in the regx */ + if (reg_idx) { + match_set_reg(&pin->flow_metadata, *reg_idx, success); + } + queue_msg(ofputil_encode_resume(pin, continuation, proto)); + if (pkt_out_ptr) { + dp_packet_uninit(pkt_out_ptr); + } +} + +static void process_packet_in(const struct ofp_header *msg) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); struct ofputil_packet_in pin; + struct ofpbuf continuation; enum ofperr error = ofputil_decode_packet_in(msg, true, &pin, - NULL, NULL, NULL); + NULL, NULL, &continuation); + if (error) { VLOG_WARN_RL(&rl, "error decoding packet-in: %s", ofperr_to_string(error)); @@ -242,6 +424,9 @@ process_packet_in(const struct ofp_header *msg) pinctrl_handle_put_arp(&pin.flow_metadata.flow, &headers); break; + case ACTION_OPCODE_PUT_DHCP_OPTS: + pinctrl_handle_put_dhcp_opts(&packet, &pin, &userdata, &continuation); + break; default: VLOG_WARN_RL(&rl, "unrecognized packet-in opcode %"PRIu32, ntohl(ah->opcode)); @@ -252,6 +437,7 @@ process_packet_in(const struct ofp_header *msg) static void pinctrl_recv(const struct ofp_header *oh, enum ofptype type) { + if (type == OFPTYPE_ECHO_REQUEST) { queue_msg(make_echo_reply(oh)); } else if (type == OFPTYPE_GET_CONFIG_REPLY) { diff --git a/ovn/lib/actions.c b/ovn/lib/actions.c index 5f0bf19..150ec4f 100644 --- a/ovn/lib/actions.c +++ b/ovn/lib/actions.c @@ -20,14 +20,18 @@ #include "actions.h" #include "byte-order.h" #include "compiler.h" +#include "ovn-dhcp.h" #include "expr.h" #include "lex.h" #include "logical-fields.h" #include "openvswitch/dynamic-string.h" #include "openvswitch/ofp-actions.h" #include "openvswitch/ofpbuf.h" +#include "packets.h" +#include "shash.h" #include "simap.h" + /* Context maintained during actions_parse(). */ struct action_context { const struct action_params *ap; /* Parameters. */ @@ -186,13 +190,15 @@ add_prerequisite(struct action_context *ctx, const char *prerequisite) } static size_t -start_controller_op(struct ofpbuf *ofpacts, enum action_opcode opcode) +start_controller_op(struct ofpbuf *ofpacts, enum action_opcode opcode, + bool pause) { size_t ofs = ofpacts->size; struct ofpact_controller *oc = ofpact_put_CONTROLLER(ofpacts); oc->max_len = UINT16_MAX; oc->reason = OFPR_ACTION; + oc->pause = pause; struct action_header ah = { .opcode = htonl(opcode) }; ofpbuf_put(ofpacts, &ah, sizeof ah); @@ -212,7 +218,7 @@ finish_controller_op(struct ofpbuf *ofpacts, size_t ofs) static void put_controller_op(struct ofpbuf *ofpacts, enum action_opcode opcode) { - size_t ofs = start_controller_op(ofpacts, opcode); + size_t ofs = start_controller_op(ofpacts, opcode, false); finish_controller_op(ofpacts, ofs); } @@ -246,7 +252,9 @@ parse_arp_action(struct action_context *ctx) * converted to OpenFlow, as its userdata. ovn-controller will convert the * packet to an ARP and then send the packet and actions back to the switch * inside an OFPT_PACKET_OUT message. */ - size_t oc_offset = start_controller_op(ctx->ofpacts, ACTION_OPCODE_ARP); + /* controller. */ + size_t oc_offset = start_controller_op(ctx->ofpacts, ACTION_OPCODE_ARP, + false); ofpacts_put_openflow_actions(inner_ofpacts.data, inner_ofpacts.size, ctx->ofpacts, OFP13_VERSION); finish_controller_op(ctx->ofpacts, oc_offset); @@ -413,6 +421,193 @@ parse_put_arp_action(struct action_context *ctx) restore_args(ctx, args, ARRAY_SIZE(args)); } +static bool +parse_dhcp_opt(struct action_context *ctx, struct ofpbuf *ofpacts) +{ + if (ctx->lexer->token.type != LEX_T_ID) { + action_syntax_error(ctx, NULL); + return false; + } + + enum lex_type lookahead = lexer_lookahead(ctx->lexer); + if (lookahead != LEX_T_EQUALS) { + action_syntax_error(ctx, NULL); + return false; + } + + const struct dhcp_opts_map *dhcp_opt = dhcp_opts_find( + ctx->ap->dhcp_opts, ctx->lexer->token.s); + + if (!dhcp_opt) { + action_syntax_error(ctx, "expecting valid dhcp option."); + return false; + } + + lexer_get(ctx->lexer); + lexer_get(ctx->lexer); + + struct expr_constant_set cs; + memset(&cs, 0, sizeof(struct expr_constant_set)); + if (!expr_parse_constant_set(ctx->lexer, NULL, &cs)) { + action_syntax_error(ctx, "invalid dhcp option values"); + return false; + } + + if (!strcmp(dhcp_opt->type, "str")) { + if (cs.type != EXPR_C_STRING) { + return false; + } + } else { + if (cs.type != EXPR_C_INTEGER) { + return false; + } + } + + if (!lexer_match(ctx->lexer, LEX_T_COMMA) && ( + ctx->lexer->token.type != LEX_T_RPAREN)) { + action_syntax_error(ctx, NULL); + return false; + } + + + if (dhcp_opt->code == 0) { + /* offer-ip */ + ofpbuf_put(ofpacts, &cs.values[0].value.ipv4, sizeof(ovs_be32)); + goto exit; + } + + uint8_t *opt_header = ofpbuf_put_uninit(ofpacts, 2); + opt_header[0] = dhcp_opt->code; + + if (!strcmp(dhcp_opt->type, "bool") || !strcmp(dhcp_opt->type, "uint8")) { + opt_header[1] = 1; + ofpbuf_put(ofpacts, &cs.values[0].value.u8_val, 1); + } else if (!strcmp(dhcp_opt->type, "uint16")) { + opt_header[1] = 2; + ofpbuf_put(ofpacts, &cs.values[0].value.be16_int, 2); + } else if (!strcmp(dhcp_opt->type, "uint32")) { + opt_header[1] = 4; + ofpbuf_put(ofpacts, &cs.values[0].value.be32_int, 4); + } else if (!strcmp(dhcp_opt->type, "ipv4")) { + opt_header[1] = cs.n_values * sizeof(ovs_be32); + for (size_t i = 0; i < cs.n_values; i++) { + ofpbuf_put(ofpacts, &cs.values[i].value.ipv4, sizeof(ovs_be32)); + } + } else if (!strcmp(dhcp_opt->type, "static_routes")) { + size_t no_of_routes = cs.n_values; + if (no_of_routes % 2) { + no_of_routes -= 1; + } + opt_header[1] = 0; + + /* Calculating the length of this option first because when + * we call ofpbuf_put, it might reallocate the buffer if the + * tail room is short making "opt_header" pointer invalid. + * So running the for loop twice. + */ + for (size_t i = 0; i < no_of_routes; i += 2) { + uint8_t plen = 32; + if (cs.values[i].masked) { + plen = (uint8_t) ip_count_cidr_bits(cs.values[i].mask.ipv4); + } + opt_header[1] += (1 + (plen / 8) + sizeof(ovs_be32)) ; + } + + /* Copied from RFC 3442. Please refer to this RFC for the format of + * the classless static route option. + * + * The following table contains some examples of how various subnet + * number/mask combinations can be encoded: + * + * Subnet number Subnet mask Destination descriptor + * 0 0 0 + * 10.0.0.0 255.0.0.0 8.10 + * 10.0.0.0 255.255.255.0 24.10.0.0 + * 10.17.0.0 255.255.0.0 16.10.17 + * 10.27.129.0 255.255.255.0 24.10.27.129 + * 10.229.0.128 255.255.255.128 25.10.229.0.128 + * 10.198.122.47 255.255.255.255 32.10.198.122.47 + */ + + for (size_t i = 0; i < no_of_routes; i += 2) { + uint8_t plen = 32; + if (cs.values[i].masked) { + plen = (uint8_t) ip_count_cidr_bits(cs.values[i].mask.ipv4); + } + ofpbuf_put(ofpacts, &plen, 1); + ofpbuf_put(ofpacts, &cs.values[i].value.ipv4, plen / 8); + ofpbuf_put(ofpacts, &cs.values[i + 1].value.ipv4, + sizeof(ovs_be32)); + } + } else if (!strcmp(dhcp_opt->type, "str")) { + opt_header[1] = strlen(cs.values[0].string); + ofpbuf_put(ofpacts, cs.values[0].string, opt_header[1]); + } + +exit: + expr_constant_set_destroy(&cs); + return true; +} + +/* + * Parses the "put_dhcp_opts" action. + * Format of the action is + * - put_dchp_opts(R, offer_ip = , D1 = V1,..., Dn = Vn) + * + * Example: + * put_dhcp_opts(reg0, offer_ip = 10.0.0.4, router = 10.0.0.1, + * netmask = 255.255.255.0, lease_time = 3600,...) + * + * The parsed data is stored in the ofpact_controller->userdata. + * + * The format of the "userdata" is + * -------------------------------------------------------------------------- + * | PUT_DHCP_OPTS OP CODE | REG IDX (4 B) | OFFER IP | DHCP options | + * | (8 B) |(to store the result)| (4 B) | | + * -------------------------------------------------------------------------- + */ +static void +parse_put_dhcp_opts_action(struct action_context *ctx) +{ + if (!action_force_match(ctx, LEX_T_LPAREN)) { + return; + } + + const struct expr_symbol *symbol = + shash_find_data(ctx->ap->symtab, ctx->lexer->token.s); + + /* Allowed registers to store the result are reg0 to reg2 */ + if (!symbol || ( + symbol->field->id < MFF_REG0 && symbol->field->id > MFF_REG2)) { + return; + } + + lexer_get(ctx->lexer); + if (!action_force_match(ctx, LEX_T_COMMA)) { + return; + } + + /* Make sure the first option is "offer_ip" */ + const struct dhcp_opts_map *dhcp_opt = dhcp_opts_find( + ctx->ap->dhcp_opts, ctx->lexer->token.s); + if (!dhcp_opt || dhcp_opt->code != 0) { + return; + } + + /* controller. */ + size_t oc_offset = start_controller_op(ctx->ofpacts, + ACTION_OPCODE_PUT_DHCP_OPTS, true); + uint32_t *reg_idx = ofpbuf_put_uninit(ctx->ofpacts, sizeof(uint32_t)); + *reg_idx = symbol->field->id - MFF_REG0; + + while (!lexer_match(ctx->lexer, LEX_T_RPAREN)) { + if (!parse_dhcp_opt(ctx, ctx->ofpacts)) { + return; + } + } + finish_controller_op(ctx->ofpacts, oc_offset); +} + static void emit_ct(struct action_context *ctx, bool recirc_next, bool commit) { @@ -475,6 +670,8 @@ parse_action(struct action_context *ctx) parse_get_arp_action(ctx); } else if (lexer_match_id(ctx->lexer, "put_arp")) { parse_put_arp_action(ctx); + } else if (lexer_match_id(ctx->lexer, "put_dhcp_opts")) { + parse_put_dhcp_opts_action(ctx); } else { action_syntax_error(ctx, "expecting action"); } diff --git a/ovn/lib/actions.h b/ovn/lib/actions.h index 29af06f..77c66cf 100644 --- a/ovn/lib/actions.h +++ b/ovn/lib/actions.h @@ -44,6 +44,11 @@ enum action_opcode { * MFF_ETH_SRC = mac */ ACTION_OPCODE_PUT_ARP, + + /* "put_dhcp_opts(reg_idx, offer_ip,...dhcp actions ...)". + * + */ + ACTION_OPCODE_PUT_DHCP_OPTS, }; /* Header. */ @@ -58,6 +63,9 @@ struct action_params { * expr_parse()). */ const struct shash *symtab; + /* hmap of 'struct dhcp_opts_map' to support 'put_dhcp_opts' action */ + const struct hmap *dhcp_opts; + /* Looks up logical port 'port_name'. If found, stores its port number in * '*portp' and returns true; otherwise, returns false. */ bool (*lookup_port)(const void *aux, const char *port_name, diff --git a/ovn/lib/automake.mk b/ovn/lib/automake.mk index 2b178da..4f8cd05 100644 --- a/ovn/lib/automake.mk +++ b/ovn/lib/automake.mk @@ -10,6 +10,7 @@ ovn_lib_libovn_la_SOURCES = \ ovn/lib/expr.h \ ovn/lib/lex.c \ ovn/lib/lex.h \ + ovn/lib/ovn-dhcp.h \ ovn/lib/ovn-util.c \ ovn/lib/ovn-util.h \ ovn/lib/logical-fields.h diff --git a/ovn/lib/expr.c b/ovn/lib/expr.c index f274ab4..05a5c8e 100644 --- a/ovn/lib/expr.c +++ b/ovn/lib/expr.c @@ -405,39 +405,6 @@ expr_print(const struct expr *e) /* Parsing. */ -/* Type of a "union expr_constant" or "struct expr_constant_set". */ -enum expr_constant_type { - EXPR_C_INTEGER, - EXPR_C_STRING -}; - -/* A string or integer constant (one must know which from context). */ -union expr_constant { - /* Integer constant. - * - * The width of a constant isn't always clear, e.g. if you write "1", - * there's no way to tell whether you mean for that to be a 1-bit constant - * or a 128-bit constant or somewhere in between. */ - struct { - union mf_subvalue value; - union mf_subvalue mask; /* Only initialized if 'masked'. */ - bool masked; - - enum lex_format format; /* From the constant's lex_token. */ - }; - - /* Null-terminated string constant. */ - char *string; -}; - -/* A collection of "union expr_constant"s of the same type. */ -struct expr_constant_set { - union expr_constant *values; /* Constants. */ - size_t n_values; /* Number of constants. */ - enum expr_constant_type type; /* Type of the constants. */ - bool in_curlies; /* Whether the constants were in {}. */ -}; - /* A reference to a symbol or a subfield of a symbol. * * For string fields, ofs and n_bits are 0. */ @@ -457,7 +424,6 @@ struct expr_context { struct expr *expr_parse__(struct expr_context *); static void expr_not(struct expr *); -static void expr_constant_set_destroy(struct expr_constant_set *); static bool parse_field(struct expr_context *, struct expr_field *); static bool @@ -838,7 +804,7 @@ parse_constant_set(struct expr_context *ctx, struct expr_constant_set *cs) return ok; } -static void +void expr_constant_set_destroy(struct expr_constant_set *cs) { if (cs) { @@ -2928,3 +2894,15 @@ exit: } return ctx.error; } + +bool +expr_parse_constant_set(struct lexer *lexer, const struct shash *symtab, + struct expr_constant_set *cs) +{ + struct expr_context ctx = { + .lexer = lexer, + .symtab = symtab, + }; + + return parse_constant_set(&ctx, cs); +} diff --git a/ovn/lib/expr.h b/ovn/lib/expr.h index 1327789..99e7aaf 100644 --- a/ovn/lib/expr.h +++ b/ovn/lib/expr.h @@ -391,4 +391,41 @@ char *expr_parse_field(struct lexer *, int n_bits, bool rw, const struct shash *symtab, struct mf_subfield *, struct expr **prereqsp); +/* Type of a "union expr_constant" or "struct expr_constant_set". */ +enum expr_constant_type { + EXPR_C_INTEGER, + EXPR_C_STRING +}; + +/* A string or integer constant (one must know which from context). */ +union expr_constant { + /* Integer constant. + * + * The width of a constant isn't always clear, e.g. if you write "1", + * there's no way to tell whether you mean for that to be a 1-bit constant + * or a 128-bit constant or somewhere in between. */ + struct { + union mf_subvalue value; + union mf_subvalue mask; /* Only initialized if 'masked'. */ + bool masked; + + enum lex_format format; /* From the constant's lex_token. */ + }; + + /* Null-terminated string constant. */ + char *string; +}; + +/* A collection of "union expr_constant"s of the same type. */ +struct expr_constant_set { + union expr_constant *values; /* Constants. */ + size_t n_values; /* Number of constants. */ + enum expr_constant_type type; /* Type of the constants. */ + bool in_curlies; /* Whether the constants were in {}. */ +}; + +bool expr_parse_constant_set(struct lexer *, const struct shash *symtab, + struct expr_constant_set *cs); +void expr_constant_set_destroy(struct expr_constant_set *cs); + #endif /* ovn/expr.h */ diff --git a/ovn/lib/ovn-dhcp.h b/ovn/lib/ovn-dhcp.h new file mode 100644 index 0000000..4da614b --- /dev/null +++ b/ovn/lib/ovn-dhcp.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2016 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OVN_DHCP_H +#define OVN_DHCP_H 1 + +#include "hmap.h" +#include "hash.h" + +struct dhcp_opts_map { + struct hmap_node hmap_node; + char *name; + char *type; + size_t code; +}; + +#define DHCP_OPTION(NAME, CODE, TYPE) \ + {.name = NAME, .code = CODE, .type = TYPE} + +#define OFFERIP DHCP_OPTION("offerip", 0, "ipv4") +#define DHCP_OPT_NETMASK DHCP_OPTION("netmask", 1, "ipv4") +#define DHCP_OPT_ROUTER DHCP_OPTION("router", 3, "ipv4") +#define DHCP_OPT_DNS_SERVER DHCP_OPTION("dns_server", 6, "ipv4") +#define DHCP_OPT_LOG_SERVER DHCP_OPTION("log_server", 7, "ipv4") +#define DHCP_OPT_LPR_SERVER DHCP_OPTION("lpr_server", 9, "ipv4") +#define DHCP_OPT_SWAP_SERVER DHCP_OPTION("swap_server", 16, "ipv4") + +#define DHCP_OPT_POLICY_FILTER \ + DHCP_OPTION("policy_filter", 21, "ipv4") + +#define DHCP_OPT_ROUTER_SOLICITATION \ + DHCP_OPTION("router_solicitation", 32, "ipv4") + +#define DHCP_OPT_NIS_SERVER DHCP_OPTION("nis_server", 41, "ipv4") +#define DHCP_OPT_NTP_SERVER DHCP_OPTION("ntp_server", 42, "ipv4") +#define DHCP_OPT_SERVER_ID DHCP_OPTION("server_id", 54, "ipv4") +#define DHCP_OPT_TFTP_SERVER DHCP_OPTION("tftp_server", 66, "ipv4") + +#define DHCP_OPT_CLASSLESS_STATIC_ROUTE \ + DHCP_OPTION("classless_static_route", 121, "static_routes") +#define DHCP_OPT_MS_CLASSLESS_STATIC_ROUTE \ + DHCP_OPTION("ms_classless_static_route", 249, "static_routes") + +#define DHCP_OPT_IP_FORWARD_ENABLE DHCP_OPTION("ip_forward_enable", 19, "bool") +#define DHCP_OPT_ROUTER_DISCOVERY DHCP_OPTION("router_discovery", 31, "bool") +#define DHCP_OPT_ETHERNET_ENCAP DHCP_OPTION("ethernet_encap", 36, "bool") + +#define DHCP_OPT_DEFAULT_TTL DHCP_OPTION("default_ttl", 23, "uint8") + +#define DHCP_OPT_TCP_TTL DHCP_OPTION("tcp_ttl", 37, "uint8") +#define DHCP_OPT_MTU DHCP_OPTION("mtu", 26, "uint16") +#define DHCP_OPT_LEASE_TIME DHCP_OPTION("lease_time", 51, "uint32") +#define DHCP_OPT_T1 DHCP_OPTION("T1", 58, "uint32") +#define DHCP_OPT_T2 DHCP_OPTION("T2", 59, "uint32") + +static inline uint32_t +dhcp_opt_hash(char *opt_name) +{ + return hash_string(opt_name, 0); +} + +static inline struct dhcp_opts_map * +dhcp_opts_find(const struct hmap *dhcp_opts, char *opt_name) +{ + struct dhcp_opts_map *dhcp_opt; + HMAP_FOR_EACH_WITH_HASH (dhcp_opt, hmap_node, dhcp_opt_hash(opt_name), + dhcp_opts) { + if (!strcmp(dhcp_opt->name, opt_name)) { + return dhcp_opt; + } + } + + return NULL; +} + +static inline void +dhcp_opt_add(struct hmap *dhcp_opts, char *opt_name, size_t code, char *type) +{ + struct dhcp_opts_map *dhcp_opt = xzalloc(sizeof *dhcp_opt); + dhcp_opt->name = xstrdup(opt_name); + dhcp_opt->code = code; + dhcp_opt->type = xstrdup(type); + hmap_insert(dhcp_opts, &dhcp_opt->hmap_node, dhcp_opt_hash(opt_name)); +} + +static inline void +dhcp_opts_destroy(struct hmap *dhcp_opts) +{ + struct dhcp_opts_map *dhcp_opt; + HMAP_FOR_EACH_POP(dhcp_opt, hmap_node, dhcp_opts) { + free(dhcp_opt->name); + free(dhcp_opt->type); + free(dhcp_opt); + } + hmap_destroy(dhcp_opts); +} + +#endif /* OVN_DHCP_H */ diff --git a/ovn/ovn-sb.ovsschema b/ovn/ovn-sb.ovsschema index 06e8a07..a1343c9 100644 --- a/ovn/ovn-sb.ovsschema +++ b/ovn/ovn-sb.ovsschema @@ -1,7 +1,7 @@ { "name": "OVN_Southbound", - "version": "1.3.0", - "cksum": "654726257 5528", + "version": "1.4.0", + "cksum": "198773462 6073", "tables": { "Chassis": { "columns": { @@ -110,4 +110,16 @@ "ip": {"type": "string"}, "mac": {"type": "string"}}, "indexes": [["logical_port", "ip"]], + "isRoot": true}, + "DHCP_Options": { + "columns": { + "name": {"type": "string"}, + "code": { + "type": {"key": {"type": "integer", + "minInteger": 0, "maxInteger": 254}}}, + "type": { + "type": {"key": { + "type": "string", + "enum": ["set", ["bool", "uint8", "uint16", "uint32", + "ipv4", "static_routes", "str"]]}}}}, "isRoot": true}}} diff --git a/ovn/ovn-sb.xml b/ovn/ovn-sb.xml index 1231b4e..7515e9a 100644 --- a/ovn/ovn-sb.xml +++ b/ovn/ovn-sb.xml @@ -1022,6 +1022,46 @@

Example: put_arp(inport, arp.spa, arp.sha);

+ +
+ put_dhcp_opts(R, offer_ip = IP, D1 = V1, D2 = V2, ...,Dn = Vn); +
+ +
+

+ Parameters: Name of the OVS register R, + IPv4 address to be offered IP, DHCP option name Dn, DHCP option + value Vn. +

+ +

+ Valid only in the ingress pipeline. +

+ +

+ This action acts only on valid DHCP request packets (DHCPDISCOVER + and DHCPREQUEST) and adds the DHCP options (Dn) and + values (Vn) in the "DHCP options" field of the packet. + It also sets 0x1 to the OVS register R + indicating that the action was successful and resumes the pipeline. + + If the packet is a non-DHCP packet or other type of DHCP packet + or an invalid DHCP packet, this action sets 0x0 to the + OVS register R indicating the action was unsuccessful + and resumes the pipeline. + + reg0, reg1 and reg2 + are the allowed values for the OVS register R +

+ +

+ Example: + + put_dhcp_opts(reg0, offerip = 10.0.0.2, router = 10.0.0.1, + netmask = 255.255.255.0, dns_server = {8.8.8.8,7.7.7.7}); + +

+

@@ -1544,4 +1584,157 @@ tcp.flags = RST; The Ethernet address to which the IP is bound. + + +

+ Each row in this table stores the DHCP Options supported by native OVN + DHCP. ovn-northd populates this table with the supported + DHCP options. ovn-controller looks up this table to get the + DHCP codes of the DHCP options defined in the "put_dhcp_opts" action. + Please refer to the RFC 2132 "https://tools.ietf.org/html/rfc2132" + for the possible list of DHCP options that can be defined here. +

+ + +

+ Name of the DHCP option. +

+ +

+ Example. name="router" +

+
+ + +

+ DHCP option code for the DHCP option as defined in the RFC 2132. +

+ +

+ Example. code=3 +

+
+ + +

+ Data type of the DHCP option code. +

+ +
+
value: bool
+
+

+ This indicates that the value of the DHCP option is a bool. +

+ +

+ Example. "name=ip_forward_enable", "code=19", "type=bool". +

+ +

+ put_dhcp_opts(..., ip_forward_enable = 1,...) +

+
+ +
value: uint8
+
+

+ This indicates that the value of the DHCP option is an unsigned + int8 (8 bits) +

+ +

+ Example. "name=default_ttl", "code=23", "type=uint8". +

+ +

+ put_dhcp_opts(..., default_ttl = 50,...) +

+
+ +
value: uint16
+
+

+ This indicates that the value of the DHCP option is an unsigned + int16 (16 bits). +

+ +

+ Example. "name=mtu", "code=26", "type=uint16". +

+ +

+ put_dhcp_opts(..., mtu = 1450,...) +

+
+ +
value: uint32
+
+

+ This indicates that the value of the DHCP option is an unsigned + int32 (32 bits). +

+ +

+ Example. "name=lease_time", "code=51", "type=uint32". +

+ +

+ put_dhcp_opts(..., lease_time = 86400,...) +

+
+ +
value: ipv4
+
+

+ This indicates that the value of the DHCP option is an IPv4 + address or addresses. +

+ +

+ Example. "name=router", "code=3", "type=ipv4". +

+ +

+ put_dhcp_opts(..., router = 10.0.0.1,...) +

+ +

+ Example. "name=dns_server", "code=6", "type=ipv4". +

+ +

+ put_dhcp_opts(..., dns_server = {8.8.8.8 7.7.7.7},...) +

+
+ +
value: static_routes
+
+

+ This indicates that the value of the DHCP option contains a pair of + IPv4 route and next hop addresses. +

+ +

+ Example. "name=classless_static_route", "code=121", "type=static_routes". +

+ +

+ put_dhcp_opts(..., classless_static_route = {30.0.0.0/24,10.0.0.4,0.0.0.0/0,10.0.0.1}...) +

+
+ +
value: str
+
+

+ This indicates that the value of the DHCP option is a string. +

+ +

+ Example. "name=host_name", "code=12", "type=str". +

+
+
+
+
diff --git a/tests/automake.mk b/tests/automake.mk index 7af7f69..49a470a 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -337,6 +337,7 @@ tests_ovstest_SOURCES = \ tests/test-odp.c \ tests/test-ofpbuf.c \ tests/test-ovn.c \ + tests/test-ovn-dhcp.c \ tests/test-packets.c \ tests/test-random.c \ tests/test-rcu.c \ diff --git a/tests/ovn.at b/tests/ovn.at index 633cf35..b0c365b 100644 --- a/tests/ovn.at +++ b/tests/ovn.at @@ -2871,6 +2871,57 @@ OVS_APP_EXIT_AND_WAIT([ovsdb-server]) AT_CLEANUP +AT_SETUP([ovn -- put_dhcp_opts action]) +dhcp_options="offerip=10.0.0.4,router=10.0.0.1,netmask=255.255.254.0,mtu=1400" +echo "dhcp_options = $dhcp_options" + +expected_dhcp_opts="0a00000403040a0000010104fffffe001a020578" +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg0 $dhcp_options \ +$expected_dhcp_opts], [0], [ignore]) + +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg2 $dhcp_options \ +$expected_dhcp_opts], [0], [ignore]) + +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg3 $dhcp_options \ +$expected_dhcp_opts], [1], [ignore]) + +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg5 $dhcp_options \ +$expected_dhcp_opts], [1], [ignore]) + +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action foo $dhcp_options \ +$expected_dhcp_opts], [1], [ignore]) + +dhcp_options="offerip=10.0.0.4,router=10.0.0.1,netmask=255.255.255.0,mtu=1400,\ +ip_forward_enable=1,default_ttl=121,dns_server={8.8.8.8,7.7.7.7},\ +classless_static_route={30.0.0.0/24,10.0.0.4,40.0.0.0/16,10.0.0.6,\ +0.0.0.0/0,10.0.0.1},ethernet_encap=1" + +# offerip=10.0.0.4 --> 0a000004 +# router=10.0.0.1 --> 03040a000001 +# netmask=255.255.255.0 --> 0104ffffff00 +# mtu=1400 --> 1a020578 +# ip_forward_enable-1 --> 130101 +# default_ttl=121 --> 170179 +# dns_server={8.8.8.8,7.7.7.7} --> 06080808080807070707 +# classless_static_route= --> 7914 +# {30.0.0.0/24,10.0.0.4 --> 181e00000a000004 +# 40.0.0.0/16,10.0.0.6 --> 1028000a000006 +# 0.0.0.0/0,10.0.0.1} --> 000a000001 +# ethernet_encap=1 --> 240101 +# router_discovery=0 --> 1f0100 + +expected_dhcp_opts="0a00000403040a0000010104ffffff001a020578130101170179\ +060808080808070707077914181e00000a0000041028000a000006000a000001240101" + +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg1 $dhcp_options \ +$expected_dhcp_opts], [0], [ignore]) + +dhcp_options="offerip=10.0.0.4,invalid_options=inv_value" +expected_dhcp_opts="00" +AT_CHECK([ovstest test-ovn-put-dhcp-opts-action reg0 $dhcp_options $expected_dhcp_opts], + [1], [ignore]) + +AT_CLEANUP AT_SETUP([ovn -- 2 HVs, 2 LRs connected via LS, gateway router]) AT_KEYWORDS([ovngatewayrouter]) diff --git a/tests/test-ovn-dhcp.c b/tests/test-ovn-dhcp.c new file mode 100644 index 0000000..79cee08 --- /dev/null +++ b/tests/test-ovn-dhcp.c @@ -0,0 +1,149 @@ +/* Copyright (c) 2016 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "command-line.h" +#include "openvswitch/ofp-actions.h" +#include "ovstest.h" +#include "ovn/lib/actions.h" +#include "ovn/lib/ovn-dhcp.h" +#include "ovn/lib/expr.h" +#include "ovn/lib/logical-fields.h" +#include "shash.h" + +static void +add_logical_register(struct shash *symtab, enum mf_field_id id) +{ + char name[8]; + + snprintf(name, sizeof name, "reg%d", id - MFF_REG0); + expr_symtab_add_field(symtab, name, id, NULL, false); +} + +static void +test_put_dhcp_opts_action(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + if (argc != 4) { + printf("Usage: %s reg_name dhcp-options expected-dhcp-opt-codes", + argv[0]); + exit(1); + } + + struct hmap dhcp_opts = HMAP_INITIALIZER(&dhcp_opts); + + dhcp_opt_add(&dhcp_opts, "offerip", 0, "ipv4"); + dhcp_opt_add(&dhcp_opts, "netmask", 1, "ipv4"); + dhcp_opt_add(&dhcp_opts, "router", 3, "ipv4"); + dhcp_opt_add(&dhcp_opts, "dns_server", 6, "ipv4"); + dhcp_opt_add(&dhcp_opts, "log_server", 7, "ipv4"); + dhcp_opt_add(&dhcp_opts, "lpr_server", 9, "ipv4"); + dhcp_opt_add(&dhcp_opts, "swap_server", 16, "ipv4"); + dhcp_opt_add(&dhcp_opts, "policy_filter", 21, "ipv4"); + dhcp_opt_add(&dhcp_opts, "router_solicitation", 32, "ipv4"); + dhcp_opt_add(&dhcp_opts, "nis_server", 41, "ipv4"); + dhcp_opt_add(&dhcp_opts, "ntp_server", 42, "ipv4"); + dhcp_opt_add(&dhcp_opts, "server_id", 54, "ipv4"); + dhcp_opt_add(&dhcp_opts, "tftp_server", 66, "ipv4"); + dhcp_opt_add(&dhcp_opts, "classless_static_route", 121, + "static_routes"); + dhcp_opt_add(&dhcp_opts, "ip_forward_enable", 19, "bool"); + dhcp_opt_add(&dhcp_opts, "router_discovery", 31, "bool"); + dhcp_opt_add(&dhcp_opts, "ethernet_encap", 36, "bool"); + dhcp_opt_add(&dhcp_opts, "default_ttl", 23, "uint8"); + dhcp_opt_add(&dhcp_opts, "tcp_ttl", 37, "uint8"); + dhcp_opt_add(&dhcp_opts, "mtu", 26, "uint16"); + dhcp_opt_add(&dhcp_opts, "lease_time", 51, "uint32"); + + struct shash symtab; + shash_init(&symtab); +#define MFF_LOG_REG(ID) add_logical_register(&symtab, ID); + MFF_LOG_REGS; +#undef MFF_LOG_REG + + struct action_params ap = { + .symtab = &symtab, + .dhcp_opts = &dhcp_opts, + }; + + + char *actions = xasprintf("put_dhcp_opts(%s, %s);", argv[1], argv[2]); + uint64_t ofpacts_stub[128 / 8]; + struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER( + ofpacts_stub); + struct expr *prereqs; + char *error; + + error = actions_parse_string(actions, &ap, &ofpacts, &prereqs); + dhcp_opts_destroy(&dhcp_opts); + free(actions); + if (error) { + printf("actions_parse_string failed with error - %s\n", error); + free(error); + exit(1); + } + + if (ofpacts.size < (sizeof(struct ofpact_controller) + + sizeof(struct action_header))) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : " + " ofpact_controller not configured"); + } + + struct ofpact_controller *oc = ofpbuf_pull(&ofpacts, sizeof *oc); + if (!oc->pause) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : pause flag " + " not set in ofpact_controller"); + } + struct action_header *ah = ofpbuf_pull(&ofpacts, sizeof *ah); + if (ah->opcode != htonl(ACTION_OPCODE_PUT_DHCP_OPTS)) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : put_dhcp_opts " + "action header flag not set"); + } + + uint32_t *reg_idx = ofpbuf_pull(&ofpacts, sizeof *reg_idx); + const struct mf_field *field = mf_from_name(argv[1]); + if (!field) { + ovs_fatal(1, "Error. Invalid register name : %s", argv[1]); + } + + if (*reg_idx != (field->id - MFF_REG0)) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : status register " + "id in userdata doesn't match. Expected - [%d] :" + " Actual - [%d]\n", field->id - MFF_REG0, *reg_idx); + } + + uint64_t expected_dhcp_opts_stub[128 / 8]; + struct ofpbuf expected_dhcp_opts = OFPBUF_STUB_INITIALIZER( + expected_dhcp_opts_stub); + if (ofpbuf_put_hex(&expected_dhcp_opts, argv[3], NULL)[0] != '\0') { + ovs_fatal(1, "Error. Invalid expected dhcp opts"); + } + + if (oc->userdata_len != + (expected_dhcp_opts.size + sizeof *ah + sizeof(uint32_t))) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : userdata length" + " mismatch. Expected - %"PRIuSIZE" : Actual - %"PRIu16"", + expected_dhcp_opts.size + sizeof *ah, oc->userdata_len); + } + + if (memcmp(ofpacts.data, expected_dhcp_opts.data, expected_dhcp_opts.size)) { + ovs_fatal(1, "Error. put_dhcp_opts parse action failed : dhcp opts are" + " not as expected"); + } + + exit(0); +} + +OVSTEST_REGISTER("test-ovn-put-dhcp-opts-action", test_put_dhcp_opts_action);