+/* Scans a string 's' of flags to determine their numerical value and
+ * returns the number of characters parsed using 'bit_to_string' to
+ * lookup flag names. Scanning continues until the character 'end' is
+ * reached.
+ *
+ * In the event of a failure, a negative error code will be returned. In
+ * addition, if 'res_string' is non-NULL then a descriptive string will
+ * be returned incorporating the identifying string 'field_name'. This
+ * error string must be freed by the caller.
+ *
+ * Upon success, the flag values will be stored in 'res_flags' and
+ * optionally 'res_mask', if it is non-NULL (if it is NULL then any masks
+ * present in the original string will be considered an error). The
+ * caller may restrict the acceptable set of values through the mask
+ * 'allowed'. */
+int
+parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
+ char end, const char *field_name, char **res_string,
+ uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
+{
+ uint32_t result = 0;
+ int n;
+
+ /* Parse masked flags in numeric format? */
+ if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
+ res_flags, res_mask, &n) && n > 0) {
+ if (*res_flags & ~allowed || *res_mask & ~allowed) {
+ goto unknown;
+ }
+ return n;
+ }
+
+ n = 0;
+
+ if (res_mask && (*s == '+' || *s == '-')) {
+ uint32_t flags = 0, mask = 0;
+
+ /* Parse masked flags. */
+ while (s[0] != end) {
+ bool set;
+ uint32_t bit;
+ size_t len;
+
+ if (s[0] == '+') {
+ set = true;
+ } else if (s[0] == '-') {
+ set = false;
+ } else {
+ if (res_string) {
+ *res_string = xasprintf("%s: %s must be preceded by '+' "
+ "(for SET) or '-' (NOT SET)", s,
+ field_name);
+ }
+ return -EINVAL;
+ }
+ s++;
+ n++;
+
+ for (bit = 1; bit; bit <<= 1) {
+ const char *fname = bit_to_string(bit);
+
+ if (!fname) {
+ continue;
+ }
+
+ len = strlen(fname);
+ if (strncmp(s, fname, len) ||
+ (s[len] != '+' && s[len] != '-' && s[len] != end)) {
+ continue;
+ }
+
+ if (mask & bit) {
+ /* bit already set. */
+ if (res_string) {
+ *res_string = xasprintf("%s: Each %s flag can be "
+ "specified only once", s,
+ field_name);
+ }
+ return -EINVAL;
+ }
+ if (!(bit & allowed)) {
+ goto unknown;
+ }
+ if (set) {
+ flags |= bit;
+ }
+ mask |= bit;
+ break;
+ }
+
+ if (!bit) {
+ goto unknown;
+ }
+ s += len;
+ n += len;
+ }
+
+ *res_flags = flags;
+ *res_mask = mask;
+ return n;
+ }
+
+ /* Parse unmasked flags. If a flag is present, it is set, otherwise
+ * it is not set. */
+ while (s[n] != end) {
+ unsigned long long int flags;
+ uint32_t bit;
+ int n0;
+
+ if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
+ if (flags & ~allowed) {
+ goto unknown;
+ }
+ n += n0 + (s[n + n0] == '|');
+ result |= flags;
+ continue;
+ }
+
+ for (bit = 1; bit; bit <<= 1) {
+ const char *name = bit_to_string(bit);
+ size_t len;
+
+ if (!name) {
+ continue;
+ }
+
+ len = strlen(name);
+ if (!strncmp(s + n, name, len) &&
+ (s[n + len] == '|' || s[n + len] == end)) {
+ if (!(bit & allowed)) {
+ goto unknown;
+ }
+ result |= bit;
+ n += len + (s[n + len] == '|');
+ break;
+ }
+ }
+
+ if (!bit) {
+ goto unknown;
+ }
+ }
+
+ *res_flags = result;
+ if (res_mask) {
+ *res_mask = UINT32_MAX;
+ }
+ if (res_string) {
+ *res_string = NULL;
+ }
+ return n;
+
+unknown:
+ if (res_string) {
+ *res_string = xasprintf("%s: unknown %s flag(s)", s, field_name);
+ }
+ return -EINVAL;
+}
+
+void
+flow_format(struct ds *ds, const struct flow *flow)
+{
+ struct match match;
+ struct flow_wildcards *wc = &match.wc;
+
+ match_wc_init(&match, flow);
+
+ /* As this function is most often used for formatting a packet in a
+ * packet-in message, skip formatting the packet context fields that are
+ * all-zeroes to make the print-out easier on the eyes. This means that a
+ * missing context field implies a zero value for that field. This is
+ * similar to OpenFlow encoding of these fields, as the specification
+ * states that all-zeroes context fields should not be encoded in the
+ * packet-in messages. */
+ if (!flow->in_port.ofp_port) {
+ WC_UNMASK_FIELD(wc, in_port);
+ }
+ if (!flow->skb_priority) {
+ WC_UNMASK_FIELD(wc, skb_priority);
+ }
+ if (!flow->pkt_mark) {
+ WC_UNMASK_FIELD(wc, pkt_mark);
+ }
+ if (!flow->recirc_id) {
+ WC_UNMASK_FIELD(wc, recirc_id);
+ }
+ if (!flow->dp_hash) {
+ WC_UNMASK_FIELD(wc, dp_hash);
+ }
+ if (!flow->ct_state) {
+ WC_UNMASK_FIELD(wc, ct_state);
+ }
+ if (!flow->ct_zone) {
+ WC_UNMASK_FIELD(wc, ct_zone);
+ }
+ if (!flow->ct_mark) {
+ WC_UNMASK_FIELD(wc, ct_mark);
+ }
+ if (ovs_u128_is_zero(&flow->ct_label)) {
+ WC_UNMASK_FIELD(wc, ct_label);
+ }
+ for (int i = 0; i < FLOW_N_REGS; i++) {
+ if (!flow->regs[i]) {
+ WC_UNMASK_FIELD(wc, regs[i]);
+ }
+ }
+ if (!flow->metadata) {
+ WC_UNMASK_FIELD(wc, metadata);
+ }
+
+ match_format(&match, ds, OFP_DEFAULT_PRIORITY);
+}
+
+void
+flow_print(FILE *stream, const struct flow *flow)
+{
+ char *s = flow_to_string(flow);
+ fputs(s, stream);
+ free(s);
+}
+\f
+/* flow_wildcards functions. */
+
+/* Initializes 'wc' as a set of wildcards that matches every packet. */
+void
+flow_wildcards_init_catchall(struct flow_wildcards *wc)
+{
+ memset(&wc->masks, 0, sizeof wc->masks);
+}
+
+/* Converts a flow into flow wildcards. It sets the wildcard masks based on
+ * the packet headers extracted to 'flow'. It will not set the mask for fields
+ * that do not make sense for the packet type. OpenFlow-only metadata is
+ * wildcarded, but other metadata is unconditionally exact-matched. */
+void flow_wildcards_init_for_packet(struct flow_wildcards *wc,
+ const struct flow *flow)
+{
+ memset(&wc->masks, 0x0, sizeof wc->masks);
+
+ /* Update this function whenever struct flow changes. */
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
+
+ if (flow->tunnel.ip_dst) {
+ if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
+ WC_MASK_FIELD(wc, tunnel.tun_id);
+ }
+ WC_MASK_FIELD(wc, tunnel.ip_src);
+ WC_MASK_FIELD(wc, tunnel.ip_dst);
+ WC_MASK_FIELD(wc, tunnel.flags);
+ WC_MASK_FIELD(wc, tunnel.ip_tos);
+ WC_MASK_FIELD(wc, tunnel.ip_ttl);
+ WC_MASK_FIELD(wc, tunnel.tp_src);
+ WC_MASK_FIELD(wc, tunnel.tp_dst);
+ WC_MASK_FIELD(wc, tunnel.gbp_id);
+ WC_MASK_FIELD(wc, tunnel.gbp_flags);
+
+ if (!(flow->tunnel.flags & FLOW_TNL_F_UDPIF)) {
+ if (flow->tunnel.metadata.present.map) {
+ wc->masks.tunnel.metadata.present.map =
+ flow->tunnel.metadata.present.map;
+ WC_MASK_FIELD(wc, tunnel.metadata.opts.u8);
+ }
+ } else {
+ WC_MASK_FIELD(wc, tunnel.metadata.present.len);
+ memset(wc->masks.tunnel.metadata.opts.gnv, 0xff,
+ flow->tunnel.metadata.present.len);
+ }
+ } else if (flow->tunnel.tun_id) {
+ WC_MASK_FIELD(wc, tunnel.tun_id);
+ }
+
+ /* metadata, regs, and conj_id wildcarded. */
+
+ WC_MASK_FIELD(wc, skb_priority);
+ WC_MASK_FIELD(wc, pkt_mark);
+ WC_MASK_FIELD(wc, ct_state);
+ WC_MASK_FIELD(wc, ct_zone);
+ WC_MASK_FIELD(wc, ct_mark);
+ WC_MASK_FIELD(wc, ct_label);
+ WC_MASK_FIELD(wc, recirc_id);
+ WC_MASK_FIELD(wc, dp_hash);
+ WC_MASK_FIELD(wc, in_port);
+
+ /* actset_output wildcarded. */
+
+ WC_MASK_FIELD(wc, dl_dst);
+ WC_MASK_FIELD(wc, dl_src);
+ WC_MASK_FIELD(wc, dl_type);
+ WC_MASK_FIELD(wc, vlan_tci);
+
+ if (flow->dl_type == htons(ETH_TYPE_IP)) {
+ WC_MASK_FIELD(wc, nw_src);
+ WC_MASK_FIELD(wc, nw_dst);
+ } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
+ WC_MASK_FIELD(wc, ipv6_src);
+ WC_MASK_FIELD(wc, ipv6_dst);
+ WC_MASK_FIELD(wc, ipv6_label);
+ } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
+ flow->dl_type == htons(ETH_TYPE_RARP)) {
+ WC_MASK_FIELD(wc, nw_src);
+ WC_MASK_FIELD(wc, nw_dst);
+ WC_MASK_FIELD(wc, nw_proto);
+ WC_MASK_FIELD(wc, arp_sha);
+ WC_MASK_FIELD(wc, arp_tha);
+ return;
+ } else if (eth_type_mpls(flow->dl_type)) {
+ for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
+ WC_MASK_FIELD(wc, mpls_lse[i]);
+ if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
+ break;
+ }
+ }
+ return;
+ } else {
+ return; /* Unknown ethertype. */
+ }
+
+ /* IPv4 or IPv6. */
+ WC_MASK_FIELD(wc, nw_frag);
+ WC_MASK_FIELD(wc, nw_tos);
+ WC_MASK_FIELD(wc, nw_ttl);
+ WC_MASK_FIELD(wc, nw_proto);
+
+ /* No transport layer header in later fragments. */
+ if (!(flow->nw_frag & FLOW_NW_FRAG_LATER) &&
+ (flow->nw_proto == IPPROTO_ICMP ||
+ flow->nw_proto == IPPROTO_ICMPV6 ||
+ flow->nw_proto == IPPROTO_TCP ||
+ flow->nw_proto == IPPROTO_UDP ||
+ flow->nw_proto == IPPROTO_SCTP ||
+ flow->nw_proto == IPPROTO_IGMP)) {
+ WC_MASK_FIELD(wc, tp_src);
+ WC_MASK_FIELD(wc, tp_dst);
+
+ if (flow->nw_proto == IPPROTO_TCP) {
+ WC_MASK_FIELD(wc, tcp_flags);
+ } else if (flow->nw_proto == IPPROTO_ICMPV6) {
+ WC_MASK_FIELD(wc, arp_sha);
+ WC_MASK_FIELD(wc, arp_tha);
+ WC_MASK_FIELD(wc, nd_target);
+ } else if (flow->nw_proto == IPPROTO_IGMP) {
+ WC_MASK_FIELD(wc, igmp_group_ip4);
+ }
+ }
+}
+
+/* Return a map of possible fields for a packet of the same type as 'flow'.
+ * Including extra bits in the returned mask is not wrong, it is just less
+ * optimal.
+ *
+ * This is a less precise version of flow_wildcards_init_for_packet() above. */