X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fnx-match.c;h=2654dde07fc325d019e82c77172a4036291d6326;hb=a281173e19918379832ec535b18aa2af58832efb;hp=f66c6556a8f12de051288df332755e3d470010f3;hpb=db7f8281824224289eeb177c5437c9ec070ab042;p=openvswitch diff --git a/lib/nx-match.c b/lib/nx-match.c index f66c6556..2654dde0 100644 --- a/lib/nx-match.c +++ b/lib/nx-match.c @@ -162,6 +162,8 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f, struct flow_wildcards *wc = &rule->wc; struct flow *flow = &rule->flow; + BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1); + switch (f->index) { /* Metadata. */ case NFI_NXM_OF_IN_PORT: @@ -345,7 +347,7 @@ parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f, /* IPv6 Neighbor Discovery. */ case NFI_NXM_NX_ND_TARGET: /* We've already verified that it's an ICMPv6 message. */ - if ((flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)) + if ((flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)) && (flow->tp_src != htons(ND_NEIGHBOR_ADVERT))) { return NXM_BAD_PREREQ; } @@ -706,6 +708,8 @@ nx_put_match(struct ofpbuf *b, const struct cls_rule *cr) int match_len; int i; + BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1); + /* Metadata. */ if (!(wc & FWW_IN_PORT)) { uint16_t in_port = flow->in_port; @@ -1187,42 +1191,72 @@ int nxm_check_reg_move(const struct nx_action_reg_move *action, const struct flow *flow) { - const struct nxm_field *src; - const struct nxm_field *dst; + int src_ofs, dst_ofs, n_bits; + int error; - if (action->n_bits == htons(0)) { - return BAD_ARGUMENT; - } + n_bits = ntohs(action->n_bits); + src_ofs = ntohs(action->src_ofs); + dst_ofs = ntohs(action->dst_ofs); - src = nxm_field_lookup(ntohl(action->src)); - if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) { - return BAD_ARGUMENT; + error = nxm_src_check(action->src, src_ofs, n_bits, flow); + if (error) { + return error; } - dst = nxm_field_lookup(ntohl(action->dst)); - if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) { - return BAD_ARGUMENT; + return nxm_dst_check(action->dst, dst_ofs, n_bits, flow); +} + +/* Given a flow, checks that the source field represented by 'src_header' + * in the range ['ofs', 'ofs' + 'n_bits') is valid. */ +int +nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits, + const struct flow *flow) +{ + const struct nxm_field *src = nxm_field_lookup(ntohl(src_header)); + + if (!n_bits) { + VLOG_WARN_RL(&rl, "zero bit source field"); + } else if (!field_ok(src, flow, ofs + n_bits)) { + VLOG_WARN_RL(&rl, "invalid source field"); + } else { + return 0; } - if (!dst->writable) { - return BAD_ARGUMENT; + return BAD_ARGUMENT; +} + +/* Given a flow, checks that the destination field represented by 'dst_header' + * in the range ['ofs', 'ofs' + 'n_bits') is valid. */ +int +nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits, + const struct flow *flow) +{ + const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header)); + + if (!n_bits) { + VLOG_WARN_RL(&rl, "zero bit destination field"); + } else if (!field_ok(dst, flow, ofs + n_bits)) { + VLOG_WARN_RL(&rl, "invalid destination field"); + } else if (!dst->writable) { + VLOG_WARN_RL(&rl, "destination field is not writable"); + } else { + return 0; } - return 0; + return BAD_ARGUMENT; } int nxm_check_reg_load(const struct nx_action_reg_load *action, const struct flow *flow) { - const struct nxm_field *dst; - int ofs, n_bits; + unsigned int ofs = nxm_decode_ofs(action->ofs_nbits); + unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits); + int error; - ofs = nxm_decode_ofs(action->ofs_nbits); - n_bits = nxm_decode_n_bits(action->ofs_nbits); - dst = nxm_field_lookup(ntohl(action->dst)); - if (!field_ok(dst, flow, ofs + n_bits)) { - return BAD_ARGUMENT; + error = nxm_dst_check(action->dst, ofs, n_bits, flow); + if (error) { + return error; } /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in @@ -1231,10 +1265,6 @@ nxm_check_reg_load(const struct nx_action_reg_load *action, return BAD_ARGUMENT; } - if (!dst->writable) { - return BAD_ARGUMENT; - } - return 0; } @@ -1340,11 +1370,36 @@ nxm_read_field(const struct nxm_field *src, const struct flow *flow) NOT_REACHED(); } +/* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits' + * in 'flow'. */ +uint64_t +nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits, + const struct flow *flow) +{ + int n_bits = nxm_decode_n_bits(ofs_nbits); + int ofs = nxm_decode_ofs(ofs_nbits); + uint64_t mask, data; + + mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1; + data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow); + data = (data >> ofs) & mask; + + return data; +} + static void nxm_write_field(const struct nxm_field *dst, struct flow *flow, uint64_t new_value) { switch (dst->index) { + case NFI_NXM_OF_ETH_DST: + eth_addr_from_uint64(new_value, flow->dl_dst); + break; + + case NFI_NXM_OF_ETH_SRC: + eth_addr_from_uint64(new_value, flow->dl_src); + break; + case NFI_NXM_OF_VLAN_TCI: flow->vlan_tci = htons(new_value); break; @@ -1374,21 +1429,34 @@ nxm_write_field(const struct nxm_field *dst, struct flow *flow, #error #endif - case NFI_NXM_OF_IN_PORT: - case NFI_NXM_OF_ETH_DST: - case NFI_NXM_OF_ETH_SRC: - case NFI_NXM_OF_ETH_TYPE: case NFI_NXM_OF_IP_TOS: - case NFI_NXM_OF_IP_PROTO: - case NFI_NXM_OF_ARP_OP: + flow->nw_tos = new_value & IP_DSCP_MASK; + break; + case NFI_NXM_OF_IP_SRC: - case NFI_NXM_OF_ARP_SPA: + flow->nw_src = htonl(new_value); + break; + case NFI_NXM_OF_IP_DST: - case NFI_NXM_OF_ARP_TPA: + flow->nw_dst = htonl(new_value); + break; + case NFI_NXM_OF_TCP_SRC: case NFI_NXM_OF_UDP_SRC: + flow->tp_src = htons(new_value); + break; + case NFI_NXM_OF_TCP_DST: case NFI_NXM_OF_UDP_DST: + flow->tp_dst = htons(new_value); + break; + + case NFI_NXM_OF_IN_PORT: + case NFI_NXM_OF_ETH_TYPE: + case NFI_NXM_OF_IP_PROTO: + case NFI_NXM_OF_ARP_OP: + case NFI_NXM_OF_ARP_SPA: + case NFI_NXM_OF_ARP_TPA: case NFI_NXM_OF_ICMP_TYPE: case NFI_NXM_OF_ICMP_CODE: case NFI_NXM_NX_TUN_ID_W: @@ -1418,40 +1486,37 @@ void nxm_execute_reg_move(const struct nx_action_reg_move *action, struct flow *flow) { - /* Preparation. */ - int n_bits = ntohs(action->n_bits); - uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1; - - /* Get the interesting bits of the source field. */ - const struct nxm_field *src = nxm_field_lookup(ntohl(action->src)); - int src_ofs = ntohs(action->src_ofs); - uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs); + ovs_be16 src_ofs_nbits, dst_ofs_nbits; + uint64_t src_data; + int n_bits; - /* Get the remaining bits of the destination field. */ - const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst)); - int dst_ofs = ntohs(action->dst_ofs); - uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs); - - /* Get the final value. */ - uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs); + n_bits = ntohs(action->n_bits); + src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits); + dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits); - nxm_write_field(dst, flow, new_data); + src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow); + nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow); } void nxm_execute_reg_load(const struct nx_action_reg_load *action, struct flow *flow) { - /* Preparation. */ - int n_bits = nxm_decode_n_bits(action->ofs_nbits); - uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1; + nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow); +} - /* Get source data. */ - uint64_t src_data = ntohll(action->value); +/* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies + * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */ +void +nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data, + struct flow *flow) +{ + int n_bits = nxm_decode_n_bits(ofs_nbits); + int dst_ofs = nxm_decode_ofs(ofs_nbits); + uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1; /* Get remaining bits of the destination field. */ - const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst)); - int dst_ofs = nxm_decode_ofs(action->ofs_nbits); + const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header)); uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs); /* Get the final value. */