Coverage Report

Created: 2026-02-26 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/ofp-match.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008-2017 Nicira, Inc.
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 "openvswitch/ofp-match.h"
19
#include "byte-order.h"
20
#include "flow.h"
21
#include "nx-match.h"
22
#include "openvswitch/match.h"
23
#include "openvswitch/ofp-errors.h"
24
#include "openvswitch/ofp-msgs.h"
25
#include "openvswitch/ofp-port.h"
26
#include "openvswitch/packets.h"
27
#include "openvswitch/vlog.h"
28
29
VLOG_DEFINE_THIS_MODULE(ofp_match);
30
31
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
32
33
/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
34
 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
35
 * is wildcarded.
36
 *
37
 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
38
 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
39
 * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
40
 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
41
 * wildcarded. */
42
static ovs_be32
43
ofputil_wcbits_to_netmask(int wcbits)
44
84.9k
{
45
84.9k
    wcbits &= 0x3f;
46
84.9k
    return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
47
84.9k
}
48
49
/* Given the IP netmask 'netmask', returns the number of bits of the IP address
50
 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
51
 * between 0 and 32 inclusive.
52
 *
53
 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
54
 * still be in the valid range but isn't otherwise meaningful. */
55
static int
56
ofputil_netmask_to_wcbits(ovs_be32 netmask)
57
14.9k
{
58
14.9k
    return 32 - ip_count_cidr_bits(netmask);
59
14.9k
}
60
61
/* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
62
 * flow_wildcards in 'wc' for use in struct match.  It is the caller's
63
 * responsibility to handle the special case where the flow match's dl_vlan is
64
 * set to OFP_VLAN_NONE. */
65
void
66
ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
67
41.4k
{
68
41.4k
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
69
70
    /* Initialize most of wc. */
71
41.4k
    flow_wildcards_init_catchall(wc);
72
73
41.4k
    if (!(ofpfw & OFPFW10_IN_PORT)) {
74
29.7k
        wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
75
29.7k
    }
76
77
41.4k
    if (!(ofpfw & OFPFW10_NW_TOS)) {
78
33.0k
        wc->masks.nw_tos |= IP_DSCP_MASK;
79
33.0k
    }
80
81
41.4k
    if (!(ofpfw & OFPFW10_NW_PROTO)) {
82
32.4k
        wc->masks.nw_proto = UINT8_MAX;
83
32.4k
    }
84
41.4k
    wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
85
41.4k
                                                 >> OFPFW10_NW_SRC_SHIFT);
86
41.4k
    wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
87
41.4k
                                                 >> OFPFW10_NW_DST_SHIFT);
88
89
41.4k
    if (!(ofpfw & OFPFW10_TP_SRC)) {
90
27.0k
        wc->masks.tp_src = OVS_BE16_MAX;
91
27.0k
    }
92
41.4k
    if (!(ofpfw & OFPFW10_TP_DST)) {
93
30.8k
        wc->masks.tp_dst = OVS_BE16_MAX;
94
30.8k
    }
95
96
41.4k
    if (!(ofpfw & OFPFW10_DL_SRC)) {
97
33.9k
        WC_MASK_FIELD(wc, dl_src);
98
33.9k
    }
99
41.4k
    if (!(ofpfw & OFPFW10_DL_DST)) {
100
30.7k
        WC_MASK_FIELD(wc, dl_dst);
101
30.7k
    }
102
41.4k
    if (!(ofpfw & OFPFW10_DL_TYPE)) {
103
30.8k
        wc->masks.dl_type = OVS_BE16_MAX;
104
30.8k
    }
105
106
    /* VLAN TCI mask. */
107
41.4k
    if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
108
27.9k
        wc->masks.vlans[0].tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
109
27.9k
    }
110
41.4k
    if (!(ofpfw & OFPFW10_DL_VLAN)) {
111
23.3k
        wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
112
23.3k
    }
113
41.4k
}
114
115
/* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
116
void
117
ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
118
                               struct match *match)
119
41.4k
{
120
41.4k
    uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
121
122
    /* Initialize match->wc. */
123
41.4k
    memset(&match->flow, 0, sizeof match->flow);
124
41.4k
    ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
125
41.4k
    memset(&match->tun_md, 0, sizeof match->tun_md);
126
127
    /* If any fields, except in_port, are matched, then we also need to match
128
     * on the Ethernet packet_type. */
129
41.4k
    const uint32_t ofpfw_data_bits = (OFPFW10_NW_TOS | OFPFW10_NW_PROTO
130
41.4k
                                      | OFPFW10_TP_SRC | OFPFW10_TP_DST
131
41.4k
                                      | OFPFW10_DL_SRC | OFPFW10_DL_DST
132
41.4k
                                      | OFPFW10_DL_TYPE
133
41.4k
                                      | OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP);
134
41.4k
    if ((ofpfw & ofpfw_data_bits) != ofpfw_data_bits
135
1.14k
        || ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_SRC_SHIFT)
136
40.4k
        || ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_DST_SHIFT)) {
137
40.4k
        match_set_default_packet_type(match);
138
40.4k
    }
139
140
    /* Initialize most of match->flow. */
141
41.4k
    match->flow.nw_src = ofmatch->nw_src;
142
41.4k
    match->flow.nw_dst = ofmatch->nw_dst;
143
41.4k
    match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
144
41.4k
    match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
145
41.4k
    match->flow.tp_src = ofmatch->tp_src;
146
41.4k
    match->flow.tp_dst = ofmatch->tp_dst;
147
41.4k
    match->flow.dl_src = ofmatch->dl_src;
148
41.4k
    match->flow.dl_dst = ofmatch->dl_dst;
149
41.4k
    match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
150
41.4k
    match->flow.nw_proto = ofmatch->nw_proto;
151
152
    /* Translate VLANs. */
153
41.4k
    if (!(ofpfw & OFPFW10_DL_VLAN) &&
154
23.3k
        ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
155
        /* Match only packets without 802.1Q header.
156
         *
157
         * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
158
         *
159
         * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
160
         * because we can't have a specific PCP without an 802.1Q header.
161
         * However, older versions of OVS treated this as matching packets
162
         * withut an 802.1Q header, so we do here too. */
163
2.22k
        match->flow.vlans[0].tci = htons(0);
164
2.22k
        match->wc.masks.vlans[0].tci = htons(0xffff);
165
39.1k
    } else {
166
39.1k
        ovs_be16 vid, pcp, tci;
167
39.1k
        uint16_t hpcp;
168
169
39.1k
        vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
170
39.1k
        hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
171
39.1k
        pcp = htons(hpcp);
172
39.1k
        tci = vid | pcp | htons(VLAN_CFI);
173
39.1k
        match->flow.vlans[0].tci = tci & match->wc.masks.vlans[0].tci;
174
39.1k
    }
175
176
    /* Clean up. */
177
41.4k
    match_zero_wildcarded_fields(match);
178
41.4k
}
179
180
/* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
181
void
182
ofputil_match_to_ofp10_match(const struct match *match,
183
                             struct ofp10_match *ofmatch)
184
7.45k
{
185
7.45k
    const struct flow_wildcards *wc = &match->wc;
186
7.45k
    uint32_t ofpfw;
187
188
    /* Figure out most OpenFlow wildcards. */
189
7.45k
    ofpfw = 0;
190
7.45k
    if (!wc->masks.in_port.ofp_port) {
191
5.26k
        ofpfw |= OFPFW10_IN_PORT;
192
5.26k
    }
193
7.45k
    if (!wc->masks.dl_type) {
194
3.91k
        ofpfw |= OFPFW10_DL_TYPE;
195
3.91k
    }
196
7.45k
    if (!wc->masks.nw_proto) {
197
5.33k
        ofpfw |= OFPFW10_NW_PROTO;
198
5.33k
    }
199
7.45k
    ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
200
7.45k
              << OFPFW10_NW_SRC_SHIFT);
201
7.45k
    ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
202
7.45k
              << OFPFW10_NW_DST_SHIFT);
203
7.45k
    if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
204
5.76k
        ofpfw |= OFPFW10_NW_TOS;
205
5.76k
    }
206
7.45k
    if (!wc->masks.tp_src) {
207
6.45k
        ofpfw |= OFPFW10_TP_SRC;
208
6.45k
    }
209
7.45k
    if (!wc->masks.tp_dst) {
210
6.45k
        ofpfw |= OFPFW10_TP_DST;
211
6.45k
    }
212
7.45k
    if (eth_addr_is_zero(wc->masks.dl_src)) {
213
5.26k
        ofpfw |= OFPFW10_DL_SRC;
214
5.26k
    }
215
7.45k
    if (eth_addr_is_zero(wc->masks.dl_dst)) {
216
5.26k
        ofpfw |= OFPFW10_DL_DST;
217
5.26k
    }
218
219
    /* Translate VLANs. */
220
7.45k
    ofmatch->dl_vlan = htons(0);
221
7.45k
    ofmatch->dl_vlan_pcp = 0;
222
7.45k
    if (match->wc.masks.vlans[0].tci == htons(0)) {
223
5.21k
        ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
224
5.21k
    } else if (match->wc.masks.vlans[0].tci & htons(VLAN_CFI)
225
2.24k
               && !(match->flow.vlans[0].tci & htons(VLAN_CFI))) {
226
2.09k
        ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
227
2.09k
    } else {
228
148
        if (!(match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK))) {
229
17
            ofpfw |= OFPFW10_DL_VLAN;
230
131
        } else {
231
131
            ofmatch->dl_vlan =
232
131
                htons(vlan_tci_to_vid(match->flow.vlans[0].tci));
233
131
        }
234
235
148
        if (!(match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK))) {
236
23
            ofpfw |= OFPFW10_DL_VLAN_PCP;
237
125
        } else {
238
125
            ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlans[0].tci);
239
125
        }
240
148
    }
241
242
    /* Compose most of the match structure. */
243
7.45k
    ofmatch->wildcards = htonl(ofpfw);
244
7.45k
    ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
245
7.45k
    ofmatch->dl_src = match->flow.dl_src;
246
7.45k
    ofmatch->dl_dst = match->flow.dl_dst;
247
7.45k
    ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
248
7.45k
    ofmatch->nw_src = match->flow.nw_src;
249
7.45k
    ofmatch->nw_dst = match->flow.nw_dst;
250
7.45k
    ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
251
7.45k
    ofmatch->nw_proto = match->flow.nw_proto;
252
7.45k
    ofmatch->tp_src = match->flow.tp_src;
253
7.45k
    ofmatch->tp_dst = match->flow.tp_dst;
254
7.45k
    memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
255
7.45k
    memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
256
7.45k
}
257
258
enum ofperr
259
ofputil_pull_ofp11_match(struct ofpbuf *buf, const struct tun_table *tun_table,
260
                         const struct vl_mff_map *vl_mff_map,
261
                         struct match *match, uint16_t *padded_match_len)
262
51.5k
{
263
51.5k
    struct ofp11_match_header *omh = buf->data;
264
51.5k
    uint16_t match_len;
265
266
51.5k
    if (buf->size < sizeof *omh) {
267
138
        return OFPERR_OFPBMC_BAD_LEN;
268
138
    }
269
270
51.4k
    match_len = ntohs(omh->length);
271
272
51.4k
    switch (ntohs(omh->type)) {
273
9.38k
    case OFPMT_STANDARD: {
274
9.38k
        struct ofp11_match *om;
275
276
9.38k
        if (match_len != sizeof *om || buf->size < sizeof *om) {
277
1.88k
            return OFPERR_OFPBMC_BAD_LEN;
278
1.88k
        }
279
7.49k
        om = ofpbuf_pull(buf, sizeof *om);
280
7.49k
        if (padded_match_len) {
281
25
            *padded_match_len = match_len;
282
25
        }
283
7.49k
        return ofputil_match_from_ofp11_match(om, match);
284
9.38k
    }
285
286
35.6k
    case OFPMT_OXM:
287
35.6k
        if (padded_match_len) {
288
14.2k
            *padded_match_len = ROUND_UP(match_len, 8);
289
14.2k
        }
290
35.6k
        return oxm_pull_match(buf, false, tun_table, vl_mff_map, match);
291
292
6.42k
    default:
293
6.42k
        return OFPERR_OFPBMC_BAD_TYPE;
294
51.4k
    }
295
51.4k
}
296
297
/* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
298
 * Returns 0 if successful, otherwise an OFPERR_* value. */
299
enum ofperr
300
ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
301
                               struct match *match)
302
7.49k
{
303
7.49k
    uint16_t wc = ntohl(ofmatch->wildcards);
304
7.49k
    bool ipv4, arp, rarp;
305
306
7.49k
    match_init_catchall(match);
307
7.49k
    match->flow.tunnel.metadata.tab = NULL;
308
309
7.49k
    if (!(wc & OFPFW11_IN_PORT)) {
310
3.31k
        ofp_port_t ofp_port;
311
3.31k
        enum ofperr error;
312
313
3.31k
        error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
314
3.31k
        if (error) {
315
1.16k
            return OFPERR_OFPBMC_BAD_VALUE;
316
1.16k
        }
317
2.14k
        match_set_in_port(match, ofp_port);
318
2.14k
    }
319
320
6.33k
    struct eth_addr dl_src_mask = eth_addr_invert(ofmatch->dl_src_mask);
321
6.33k
    struct eth_addr dl_dst_mask = eth_addr_invert(ofmatch->dl_dst_mask);
322
6.33k
    if (!eth_addr_is_zero(dl_src_mask) || !eth_addr_is_zero(dl_dst_mask)) {
323
6.27k
        match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
324
6.27k
        match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
325
6.27k
        match_set_default_packet_type(match);
326
6.27k
    }
327
328
6.33k
    if (!(wc & OFPFW11_DL_VLAN)) {
329
4.54k
        if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
330
            /* Match only packets without a VLAN tag. */
331
779
            match->flow.vlans[0].tci = htons(0);
332
779
            match->wc.masks.vlans[0].tci = OVS_BE16_MAX;
333
3.77k
        } else {
334
3.77k
            if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
335
                /* Match any packet with a VLAN tag regardless of VID. */
336
143
                match->flow.vlans[0].tci = htons(VLAN_CFI);
337
143
                match->wc.masks.vlans[0].tci = htons(VLAN_CFI);
338
3.62k
            } else if (ntohs(ofmatch->dl_vlan) < 4096) {
339
                /* Match only packets with the specified VLAN VID. */
340
3.44k
                match->flow.vlans[0].tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
341
3.44k
                match->wc.masks.vlans[0].tci = htons(VLAN_CFI | VLAN_VID_MASK);
342
3.44k
            } else {
343
                /* Invalid VID. */
344
185
                return OFPERR_OFPBMC_BAD_VALUE;
345
185
            }
346
347
3.58k
            if (!(wc & OFPFW11_DL_VLAN_PCP)) {
348
2.55k
                if (ofmatch->dl_vlan_pcp <= 7) {
349
2.47k
                    match->flow.vlans[0].tci |= htons(ofmatch->dl_vlan_pcp
350
2.47k
                                                  << VLAN_PCP_SHIFT);
351
2.47k
                    match->wc.masks.vlans[0].tci |= htons(VLAN_PCP_MASK);
352
2.47k
                } else {
353
                    /* Invalid PCP. */
354
80
                    return OFPERR_OFPBMC_BAD_VALUE;
355
80
                }
356
2.55k
            }
357
3.58k
        }
358
4.28k
        match_set_default_packet_type(match);
359
4.28k
    }
360
361
6.06k
    if (!(wc & OFPFW11_DL_TYPE)) {
362
5.80k
        match_set_dl_type(match,
363
5.80k
                          ofputil_dl_type_from_openflow(ofmatch->dl_type));
364
5.80k
        match_set_default_packet_type(match);
365
5.80k
    }
366
367
6.06k
    ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
368
6.06k
    arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
369
6.06k
    rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
370
371
6.06k
    if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
372
2.54k
        if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
373
            /* Invalid TOS. */
374
65
            return OFPERR_OFPBMC_BAD_VALUE;
375
65
        }
376
377
2.48k
        match_set_nw_dscp(match, ofmatch->nw_tos);
378
2.48k
    }
379
380
6.00k
    if (ipv4 || arp || rarp) {
381
3.96k
        if (!(wc & OFPFW11_NW_PROTO)) {
382
3.08k
            match_set_nw_proto(match, ofmatch->nw_proto);
383
3.08k
        }
384
3.96k
        match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
385
3.96k
        match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
386
3.96k
    }
387
388
7.62k
#define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
389
6.00k
    if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
390
2.52k
        switch (match->flow.nw_proto) {
391
1.27k
        case IPPROTO_ICMP:
392
            /* "A.2.3 Flow Match Structures" in OF1.1 says:
393
             *
394
             *    The tp_src and tp_dst fields will be ignored unless the
395
             *    network protocol specified is as TCP, UDP or SCTP.
396
             *
397
             * but I'm pretty sure we should support ICMP too, otherwise
398
             * that's a regression from OF1.0. */
399
1.27k
            if (!(wc & OFPFW11_TP_SRC)) {
400
1.25k
                uint16_t icmp_type = ntohs(ofmatch->tp_src);
401
1.25k
                if (icmp_type < 0x100) {
402
818
                    match_set_icmp_type(match, icmp_type);
403
818
                } else {
404
437
                    return OFPERR_OFPBMC_BAD_FIELD;
405
437
                }
406
1.25k
            }
407
840
            if (!(wc & OFPFW11_TP_DST)) {
408
477
                uint16_t icmp_code = ntohs(ofmatch->tp_dst);
409
477
                if (icmp_code < 0x100) {
410
250
                    match_set_icmp_code(match, icmp_code);
411
250
                } else {
412
227
                    return OFPERR_OFPBMC_BAD_FIELD;
413
227
                }
414
477
            }
415
613
            break;
416
417
613
        case IPPROTO_TCP:
418
29
        case IPPROTO_UDP:
419
569
        case IPPROTO_SCTP:
420
569
            if (!(wc & (OFPFW11_TP_SRC))) {
421
29
                match_set_tp_src(match, ofmatch->tp_src);
422
29
            }
423
569
            if (!(wc & (OFPFW11_TP_DST))) {
424
541
                match_set_tp_dst(match, ofmatch->tp_dst);
425
541
            }
426
569
            break;
427
428
675
        default:
429
            /* OF1.1 says explicitly to ignore this. */
430
675
            break;
431
2.52k
        }
432
2.52k
    }
433
434
5.33k
    if (eth_type_mpls(match->flow.dl_type)) {
435
380
        if (!(wc & OFPFW11_MPLS_LABEL)) {
436
258
            match_set_mpls_label(match, 0, ofmatch->mpls_label);
437
258
        }
438
380
        if (!(wc & OFPFW11_MPLS_TC)) {
439
315
            match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
440
315
        }
441
380
    }
442
443
5.33k
    match_set_metadata_masked(match, ofmatch->metadata,
444
5.33k
                              ~ofmatch->metadata_mask);
445
446
5.33k
    return 0;
447
6.00k
}
448
449
/* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
450
void
451
ofputil_match_to_ofp11_match(const struct match *match,
452
                             struct ofp11_match *ofmatch)
453
206
{
454
206
    uint32_t wc = 0;
455
456
206
    memset(ofmatch, 0, sizeof *ofmatch);
457
206
    ofmatch->omh.type = htons(OFPMT_STANDARD);
458
206
    ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
459
460
206
    if (!match->wc.masks.in_port.ofp_port) {
461
205
        wc |= OFPFW11_IN_PORT;
462
205
    } else {
463
1
        ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
464
1
    }
465
466
206
    ofmatch->dl_src = match->flow.dl_src;
467
206
    ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
468
206
    ofmatch->dl_dst = match->flow.dl_dst;
469
206
    ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
470
471
206
    if (match->wc.masks.vlans[0].tci == htons(0)) {
472
203
        wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
473
203
    } else if (match->wc.masks.vlans[0].tci & htons(VLAN_CFI)
474
3
               && !(match->flow.vlans[0].tci & htons(VLAN_CFI))) {
475
1
        ofmatch->dl_vlan = htons(OFPVID11_NONE);
476
1
        wc |= OFPFW11_DL_VLAN_PCP;
477
2
    } else {
478
2
        if (!(match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK))) {
479
1
            ofmatch->dl_vlan = htons(OFPVID11_ANY);
480
1
        } else {
481
1
            ofmatch->dl_vlan =
482
1
                htons(vlan_tci_to_vid(match->flow.vlans[0].tci));
483
1
        }
484
485
2
        if (!(match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK))) {
486
1
            wc |= OFPFW11_DL_VLAN_PCP;
487
1
        } else {
488
1
            ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlans[0].tci);
489
1
        }
490
2
    }
491
492
206
    if (!match->wc.masks.dl_type) {
493
156
        wc |= OFPFW11_DL_TYPE;
494
156
    } else {
495
50
        ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
496
50
    }
497
498
206
    if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
499
205
        wc |= OFPFW11_NW_TOS;
500
205
    } else {
501
1
        ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
502
1
    }
503
504
206
    if (!match->wc.masks.nw_proto) {
505
187
        wc |= OFPFW11_NW_PROTO;
506
187
    } else {
507
19
        ofmatch->nw_proto = match->flow.nw_proto;
508
19
    }
509
510
206
    ofmatch->nw_src = match->flow.nw_src;
511
206
    ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
512
206
    ofmatch->nw_dst = match->flow.nw_dst;
513
206
    ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
514
515
206
    if (!match->wc.masks.tp_src) {
516
205
        wc |= OFPFW11_TP_SRC;
517
205
    } else {
518
1
        ofmatch->tp_src = match->flow.tp_src;
519
1
    }
520
521
206
    if (!match->wc.masks.tp_dst) {
522
205
        wc |= OFPFW11_TP_DST;
523
205
    } else {
524
1
        ofmatch->tp_dst = match->flow.tp_dst;
525
1
    }
526
527
206
    if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
528
205
        wc |= OFPFW11_MPLS_LABEL;
529
205
    } else {
530
1
        ofmatch->mpls_label = htonl(mpls_lse_to_label(
531
1
                                        match->flow.mpls_lse[0]));
532
1
    }
533
534
206
    if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
535
205
        wc |= OFPFW11_MPLS_TC;
536
205
    } else {
537
1
        ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
538
1
    }
539
540
206
    ofmatch->metadata = match->flow.metadata;
541
206
    ofmatch->metadata_mask = ~match->wc.masks.metadata;
542
543
206
    ofmatch->wildcards = htonl(wc);
544
206
}
545
546
/* Returns the "typical" length of a match for 'protocol', for use in
547
 * estimating space to preallocate. */
548
int
549
ofputil_match_typical_len(enum ofputil_protocol protocol)
550
670
{
551
670
    switch (protocol) {
552
0
    case OFPUTIL_P_OF10_STD:
553
0
    case OFPUTIL_P_OF10_STD_TID:
554
0
        return sizeof(struct ofp10_match);
555
556
0
    case OFPUTIL_P_OF10_NXM:
557
0
    case OFPUTIL_P_OF10_NXM_TID:
558
0
        return NXM_TYPICAL_LEN;
559
560
206
    case OFPUTIL_P_OF11_STD:
561
206
        return sizeof(struct ofp11_match);
562
563
107
    case OFPUTIL_P_OF12_OXM:
564
464
    case OFPUTIL_P_OF13_OXM:
565
464
    case OFPUTIL_P_OF14_OXM:
566
464
    case OFPUTIL_P_OF15_OXM:
567
464
        return NXM_TYPICAL_LEN;
568
569
0
    default:
570
0
        OVS_NOT_REACHED();
571
670
    }
572
670
}
573
574
/* Appends to 'b' an struct ofp11_match_header followed by a match that
575
 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
576
 * data appended out to a multiple of 8.  'protocol' must be one that is usable
577
 * in OpenFlow 1.1 or later.
578
 *
579
 * This function can cause 'b''s data to be reallocated.
580
 *
581
 * Returns the number of bytes appended to 'b', excluding the padding.  Never
582
 * returns zero. */
583
int
584
ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
585
                        enum ofputil_protocol protocol)
586
670
{
587
670
    switch (protocol) {
588
0
    case OFPUTIL_P_OF10_STD:
589
0
    case OFPUTIL_P_OF10_STD_TID:
590
0
    case OFPUTIL_P_OF10_NXM:
591
0
    case OFPUTIL_P_OF10_NXM_TID:
592
0
        OVS_NOT_REACHED();
593
594
206
    case OFPUTIL_P_OF11_STD: {
595
206
        struct ofp11_match *om;
596
597
        /* Make sure that no padding is needed. */
598
206
        BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
599
600
206
        om = ofpbuf_put_uninit(b, sizeof *om);
601
206
        ofputil_match_to_ofp11_match(match, om);
602
206
        return sizeof *om;
603
0
    }
604
605
107
    case OFPUTIL_P_OF12_OXM:
606
464
    case OFPUTIL_P_OF13_OXM:
607
464
    case OFPUTIL_P_OF14_OXM:
608
464
    case OFPUTIL_P_OF15_OXM:
609
464
        return oxm_put_match(b, match,
610
464
                             ofputil_protocol_to_ofp_version(protocol));
611
670
    }
612
613
670
    OVS_NOT_REACHED();
614
670
}
615
616
/* Given a 'dl_type' value in the format used in struct flow, returns the
617
 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
618
 * structure. */
619
ovs_be16
620
ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
621
12.1k
{
622
12.1k
    return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
623
12.1k
            ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
624
12.1k
            : flow_dl_type);
625
12.1k
}
626
627
/* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
628
 * structure, returns the corresponding 'dl_type' value for use in struct
629
 * flow. */
630
ovs_be16
631
ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
632
47.2k
{
633
47.2k
    return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
634
47.2k
            ? htons(FLOW_DL_TYPE_NONE)
635
47.2k
            : ofp_dl_type);
636
47.2k
}
637

638
static void
639
encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
640
0
{
641
0
    struct ofputil_tlv_map *map;
642
643
0
    LIST_FOR_EACH (map, list_node, mappings) {
644
0
        struct nx_tlv_map *nx_map;
645
646
0
        nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
647
0
        nx_map->option_class = htons(map->option_class);
648
0
        nx_map->option_type = map->option_type;
649
0
        nx_map->option_len = map->option_len;
650
0
        nx_map->index = htons(map->index);
651
0
    }
652
0
}
653
654
struct ofpbuf *
655
ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
656
                                struct ofputil_tlv_table_mod *ttm)
657
0
{
658
0
    struct ofpbuf *b;
659
0
    struct nx_tlv_table_mod *nx_ttm;
660
661
0
    b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
662
0
    nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
663
0
    nx_ttm->command = htons(ttm->command);
664
0
    encode_tlv_table_mappings(b, &ttm->mappings);
665
666
0
    return b;
667
0
}
668
669
static enum ofperr
670
decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
671
                             struct ovs_list *mappings)
672
3.33k
{
673
3.33k
    ovs_list_init(mappings);
674
675
8.51k
    while (msg->size) {
676
6.82k
        struct nx_tlv_map *nx_map;
677
6.82k
        struct ofputil_tlv_map *map;
678
679
6.82k
        nx_map = ofpbuf_pull(msg, sizeof *nx_map);
680
6.82k
        map = xmalloc(sizeof *map);
681
6.82k
        ovs_list_push_back(mappings, &map->list_node);
682
683
6.82k
        map->option_class = ntohs(nx_map->option_class);
684
6.82k
        map->option_type = nx_map->option_type;
685
686
6.82k
        map->option_len = nx_map->option_len;
687
6.82k
        if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
688
1.18k
            VLOG_WARN_RL(&rl, "tlv table option length (%u) is not a "
689
1.18k
                         "valid option size", map->option_len);
690
1.18k
            ofputil_uninit_tlv_table(mappings);
691
1.18k
            return OFPERR_NXTTMFC_BAD_OPT_LEN;
692
1.18k
        }
693
694
5.63k
        map->index = ntohs(nx_map->index);
695
5.63k
        if (map->index >= max_fields) {
696
463
            VLOG_WARN_RL(&rl, "tlv table field index (%u) is too large "
697
463
                         "(max %u)", map->index, max_fields - 1);
698
463
            ofputil_uninit_tlv_table(mappings);
699
463
            return OFPERR_NXTTMFC_BAD_FIELD_IDX;
700
463
        }
701
5.63k
    }
702
703
1.69k
    return 0;
704
3.33k
}
705
706
enum ofperr
707
ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
708
                                struct ofputil_tlv_table_mod *ttm)
709
1.87k
{
710
1.87k
    struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
711
1.87k
    ofpraw_pull_assert(&msg);
712
713
1.87k
    struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
714
1.87k
    ttm->command = ntohs(nx_ttm->command);
715
1.87k
    if (ttm->command > NXTTMC_CLEAR) {
716
604
        VLOG_WARN_RL(&rl, "tlv table mod command (%u) is out of range",
717
604
                     ttm->command);
718
604
        return OFPERR_NXTTMFC_BAD_COMMAND;
719
604
    }
720
721
1.26k
    return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
722
1.26k
                                        &ttm->mappings);
723
1.87k
}
724
725
static void
726
print_tlv_table(struct ds *s, const struct ovs_list *mappings)
727
1.68k
{
728
1.68k
    struct ofputil_tlv_map *map;
729
730
1.68k
    ds_put_cstr(s, " mapping table:\n");
731
1.68k
    ds_put_cstr(s, "  class  type  length  match field\n");
732
1.68k
    ds_put_cstr(s, " ------  ----  ------  --------------");
733
734
3.00k
    LIST_FOR_EACH (map, list_node, mappings) {
735
3.00k
        ds_put_format(s, "\n %#6"PRIx16"  %#4"PRIx8"  %6"PRIu8"  "
736
3.00k
                      "tun_metadata%"PRIu16,
737
3.00k
                      map->option_class, map->option_type, map->option_len,
738
3.00k
                      map->index);
739
3.00k
    }
740
1.68k
}
741
742
void
743
ofputil_format_tlv_table_mod(struct ds *s,
744
                             const struct ofputil_tlv_table_mod *ttm)
745
745
{
746
745
    ds_put_cstr(s, "\n ");
747
748
745
    switch (ttm->command) {
749
710
    case NXTTMC_ADD:
750
710
        ds_put_cstr(s, "ADD");
751
710
        break;
752
29
    case NXTTMC_DELETE:
753
29
        ds_put_cstr(s, "DEL");
754
29
        break;
755
6
    case NXTTMC_CLEAR:
756
6
        ds_put_cstr(s, "CLEAR");
757
6
        break;
758
745
    }
759
760
745
    if (ttm->command != NXTTMC_CLEAR) {
761
739
        print_tlv_table(s, &ttm->mappings);
762
739
    }
763
745
}
764
765
struct ofpbuf *
766
ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
767
                                  struct ofputil_tlv_table_reply *ttr)
768
0
{
769
0
    struct ofpbuf *b;
770
0
    struct nx_tlv_table_reply *nx_ttr;
771
772
0
    b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
773
0
    nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
774
0
    nx_ttr->max_option_space = htonl(ttr->max_option_space);
775
0
    nx_ttr->max_fields = htons(ttr->max_fields);
776
777
0
    encode_tlv_table_mappings(b, &ttr->mappings);
778
779
0
    return b;
780
0
}
781
782
/* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'.  Returns 0
783
 * if successful, otherwise an ofperr.
784
 *
785
 * The decoder verifies that the indexes in 'ttr->mappings' are less than
786
 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
787
 * less than TUN_METADATA_NUM_OPTS. */
788
enum ofperr
789
ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
790
                                  struct ofputil_tlv_table_reply *ttr)
791
2.06k
{
792
2.06k
    struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
793
2.06k
    ofpraw_pull_assert(&msg);
794
795
2.06k
    struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
796
2.06k
    ttr->max_option_space = ntohl(nx_ttr->max_option_space);
797
2.06k
    ttr->max_fields = ntohs(nx_ttr->max_fields);
798
799
2.06k
    return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
800
2.06k
}
801
802
char * OVS_WARN_UNUSED_RESULT
803
parse_ofp_tlv_table_mod_str(struct ofputil_tlv_table_mod *ttm,
804
                               uint16_t command, const char *s,
805
                               enum ofputil_protocol *usable_protocols)
806
0
{
807
0
    *usable_protocols = OFPUTIL_P_NXM_OXM_ANY;
808
809
0
    ttm->command = command;
810
0
    ovs_list_init(&ttm->mappings);
811
812
0
    while (*s) {
813
0
        struct ofputil_tlv_map *map = xmalloc(sizeof *map);
814
0
        int n;
815
816
0
        if (*s == ',') {
817
0
            s++;
818
0
        }
819
820
0
        ovs_list_push_back(&ttm->mappings, &map->list_node);
821
822
0
        if (!ovs_scan(s, "{class=%"SCNi16",type=%"SCNi8",len=%"SCNi8"}"
823
0
                      "->tun_metadata%"SCNi16"%n",
824
0
                      &map->option_class, &map->option_type, &map->option_len,
825
0
                      &map->index, &n)) {
826
0
            ofputil_uninit_tlv_table(&ttm->mappings);
827
0
            return xstrdup("invalid tlv mapping");
828
0
        }
829
830
0
        s += n;
831
0
    }
832
833
0
    return NULL;
834
0
}
835
836
void
837
ofputil_format_tlv_table_reply(struct ds *s,
838
                               const struct ofputil_tlv_table_reply *ttr)
839
946
{
840
946
    ds_put_char(s, '\n');
841
842
946
    const struct ofputil_tlv_map *map;
843
946
    int allocated_space = 0;
844
1.56k
    LIST_FOR_EACH (map, list_node, &ttr->mappings) {
845
1.56k
        allocated_space += map->option_len;
846
1.56k
    }
847
848
946
    ds_put_format(s, " max option space=%"PRIu32" max fields=%"PRIu16"\n",
849
946
                  ttr->max_option_space, ttr->max_fields);
850
946
    ds_put_format(s, " allocated option space=%d\n", allocated_space);
851
946
    ds_put_char(s, '\n');
852
946
    print_tlv_table(s, &ttr->mappings);
853
946
}
854
855
void
856
ofputil_uninit_tlv_table(struct ovs_list *mappings)
857
3.33k
{
858
3.33k
    struct ofputil_tlv_map *map;
859
860
6.82k
    LIST_FOR_EACH_POP (map, list_node, mappings) {
861
6.82k
        free(map);
862
6.82k
    }
863
3.33k
}
864

865
static void
866
ofputil_normalize_match__(struct match *match, bool may_log)
867
48.8k
{
868
48.8k
    enum {
869
48.8k
        MAY_NW_ADDR     = 1 << 0, /* nw_src, nw_dst */
870
48.8k
        MAY_TP_ADDR     = 1 << 1, /* tp_src, tp_dst */
871
48.8k
        MAY_NW_PROTO    = 1 << 2, /* nw_proto */
872
48.8k
        MAY_IPVx        = 1 << 3, /* tos, frag, ttl */
873
48.8k
        MAY_ARP_SHA     = 1 << 4, /* arp_sha */
874
48.8k
        MAY_ARP_THA     = 1 << 5, /* arp_tha */
875
48.8k
        MAY_IPV6        = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
876
48.8k
        MAY_ND_TARGET   = 1 << 7, /* nd_target */
877
48.8k
        MAY_MPLS        = 1 << 8, /* mpls label and tc */
878
48.8k
        MAY_ETHER       = 1 << 9, /* dl_src, dl_dst */
879
48.8k
    } may_match;
880
881
48.8k
    struct flow_wildcards wc = match->wc;
882
48.8k
    ovs_be16 dl_type;
883
884
    /* Figure out what fields may be matched. */
885
    /* Check the packet_type first and extract dl_type. */
886
48.8k
    if (wc.masks.packet_type == 0 || match_has_default_packet_type(match)) {
887
48.3k
        may_match = MAY_ETHER;
888
48.3k
        dl_type = match->flow.dl_type;
889
48.3k
    } else if (wc.masks.packet_type == OVS_BE32_MAX &&
890
573
               pt_ns(match->flow.packet_type) == OFPHTN_ETHERTYPE) {
891
88
        may_match = 0;
892
88
        dl_type = pt_ns_type_be(match->flow.packet_type);
893
485
    } else {
894
485
        may_match = 0;
895
485
        dl_type = 0;
896
485
    }
897
48.8k
    if (dl_type == htons(ETH_TYPE_IP)) {
898
7.21k
        may_match |= MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
899
7.21k
        if (match->flow.nw_proto == IPPROTO_TCP ||
900
6.28k
            match->flow.nw_proto == IPPROTO_UDP ||
901
5.64k
            match->flow.nw_proto == IPPROTO_SCTP ||
902
4.98k
            match->flow.nw_proto == IPPROTO_ICMP) {
903
2.87k
            may_match |= MAY_TP_ADDR;
904
2.87k
        }
905
41.6k
    } else if (dl_type == htons(ETH_TYPE_IPV6)) {
906
2.92k
        may_match |= MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
907
2.92k
        if (match->flow.nw_proto == IPPROTO_TCP ||
908
2.55k
            match->flow.nw_proto == IPPROTO_UDP ||
909
1.96k
            match->flow.nw_proto == IPPROTO_SCTP) {
910
1.11k
            may_match |= MAY_TP_ADDR;
911
1.81k
        } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
912
743
            may_match |= MAY_TP_ADDR;
913
743
            if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
914
281
                may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
915
462
            } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
916
81
                may_match |= MAY_ND_TARGET | MAY_ARP_THA;
917
81
            }
918
743
        }
919
38.7k
    } else if (dl_type == htons(ETH_TYPE_ARP) ||
920
38.1k
               dl_type == htons(ETH_TYPE_RARP)) {
921
1.06k
        may_match |= MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
922
37.6k
    } else if (eth_type_mpls(dl_type)) {
923
418
        may_match |= MAY_MPLS;
924
418
    }
925
926
    /* Clear the fields that may not be matched. */
927
48.8k
    if (!(may_match & MAY_ETHER)) {
928
573
        wc.masks.dl_src = wc.masks.dl_dst = eth_addr_zero;
929
573
    }
930
48.8k
    if (!(may_match & MAY_NW_ADDR)) {
931
40.6k
        wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
932
40.6k
    }
933
48.8k
    if (!(may_match & MAY_TP_ADDR)) {
934
44.1k
        wc.masks.tp_src = wc.masks.tp_dst = htons(0);
935
44.1k
    }
936
48.8k
    if (!(may_match & MAY_NW_PROTO)) {
937
37.6k
        wc.masks.nw_proto = 0;
938
37.6k
    }
939
48.8k
    if (!(may_match & MAY_IPVx)) {
940
38.7k
        wc.masks.nw_tos = 0;
941
38.7k
        wc.masks.nw_ttl = 0;
942
38.7k
    }
943
48.8k
    if (!(may_match & MAY_ARP_SHA)) {
944
47.5k
        WC_UNMASK_FIELD(&wc, arp_sha);
945
47.5k
    }
946
48.8k
    if (!(may_match & MAY_ARP_THA)) {
947
47.7k
        WC_UNMASK_FIELD(&wc, arp_tha);
948
47.7k
    }
949
48.8k
    if (!(may_match & MAY_IPV6)) {
950
45.9k
        wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
951
45.9k
        wc.masks.ipv6_label = htonl(0);
952
45.9k
    }
953
48.8k
    if (!(may_match & MAY_ND_TARGET)) {
954
48.5k
        wc.masks.nd_target = in6addr_any;
955
48.5k
    }
956
48.8k
    if (!(may_match & MAY_MPLS)) {
957
48.4k
        memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
958
48.4k
    }
959
960
    /* Log any changes. */
961
48.8k
    if (!flow_wildcards_equal(&wc, &match->wc)) {
962
35.4k
        bool log = may_log && !VLOG_DROP_INFO(&rl);
963
35.4k
        char *pre = (log
964
35.4k
                     ? match_to_string(match, NULL, OFP_DEFAULT_PRIORITY)
965
35.4k
                     : NULL);
966
967
35.4k
        match->wc = wc;
968
35.4k
        match_zero_wildcarded_fields(match);
969
970
35.4k
        if (log) {
971
0
            char *post = match_to_string(match, NULL, OFP_DEFAULT_PRIORITY);
972
0
            VLOG_INFO("normalization changed ofp_match, details:");
973
0
            VLOG_INFO(" pre: %s", pre);
974
0
            VLOG_INFO("post: %s", post);
975
0
            free(pre);
976
0
            free(post);
977
0
        }
978
35.4k
    }
979
48.8k
}
980
981
/* "Normalizes" the wildcards in 'match'.  That means:
982
 *
983
 *    1. If the type of level N is known, then only the valid fields for that
984
 *       level may be specified.  For example, ARP does not have a TOS field,
985
 *       so nw_tos must be wildcarded if 'match' specifies an ARP flow.
986
 *       Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
987
 *       ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
988
 *       IPv4 flow.
989
 *
990
 *    2. If the type of level N is not known (or not understood by Open
991
 *       vSwitch), then no fields at all for that level may be specified.  For
992
 *       example, Open vSwitch does not understand SCTP, an L4 protocol, so the
993
 *       L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
994
 *       SCTP flow.
995
 *
996
 * If this function changes 'match', it logs a rate-limited informational
997
 * message. */
998
void
999
ofputil_normalize_match(struct match *match)
1000
48.8k
{
1001
48.8k
    ofputil_normalize_match__(match, true);
1002
48.8k
}
1003
1004
/* Same as ofputil_normalize_match() without the logging.  Thus, this function
1005
 * is suitable for a program's internal use, whereas ofputil_normalize_match()
1006
 * sense for use on flows received from elsewhere (so that a bug in the program
1007
 * that sent them can be reported and corrected). */
1008
void
1009
ofputil_normalize_match_quiet(struct match *match)
1010
0
{
1011
0
    ofputil_normalize_match__(match, false);
1012
0
}
1013

1014
static void OVS_PRINTF_FORMAT(5, 6)
1015
print_wild(struct ds *string, const char *leader, int is_wild,
1016
           int verbosity, const char *format, ...)
1017
0
{
1018
0
    if (is_wild && verbosity < 2) {
1019
0
        return;
1020
0
    }
1021
0
    ds_put_cstr(string, leader);
1022
0
    if (!is_wild) {
1023
0
        va_list args;
1024
1025
0
        va_start(args, format);
1026
0
        ds_put_format_valist(string, format, args);
1027
0
        va_end(args);
1028
0
    } else {
1029
0
        ds_put_char(string, '*');
1030
0
    }
1031
0
    ds_put_char(string, ',');
1032
0
}
1033
1034
static void
1035
print_wild_port(struct ds *string, const char *leader, int is_wild,
1036
                int verbosity, ofp_port_t port,
1037
                const struct ofputil_port_map *port_map)
1038
0
{
1039
0
    if (is_wild && verbosity < 2) {
1040
0
        return;
1041
0
    }
1042
0
    ds_put_cstr(string, leader);
1043
0
    if (!is_wild) {
1044
0
        ofputil_format_port(port, port_map, string);
1045
0
    } else {
1046
0
        ds_put_char(string, '*');
1047
0
    }
1048
0
    ds_put_char(string, ',');
1049
0
}
1050
1051
static void
1052
print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
1053
                 uint32_t wild_bits, int verbosity)
1054
0
{
1055
0
    if (wild_bits >= 32 && verbosity < 2) {
1056
0
        return;
1057
0
    }
1058
0
    ds_put_cstr(string, leader);
1059
0
    if (wild_bits < 32) {
1060
0
        ds_put_format(string, IP_FMT, IP_ARGS(ip));
1061
0
        if (wild_bits) {
1062
0
            ds_put_format(string, "/%d", 32 - wild_bits);
1063
0
        }
1064
0
    } else {
1065
0
        ds_put_char(string, '*');
1066
0
    }
1067
0
    ds_put_char(string, ',');
1068
0
}
1069
1070
void
1071
ofp10_match_print(struct ds *f, const struct ofp10_match *om,
1072
                  const struct ofputil_port_map *port_map, int verbosity)
1073
0
{
1074
0
    char *s = ofp10_match_to_string(om, port_map, verbosity);
1075
0
    ds_put_cstr(f, s);
1076
0
    free(s);
1077
0
}
1078
1079
char *
1080
ofp10_match_to_string(const struct ofp10_match *om,
1081
                      const struct ofputil_port_map *port_map, int verbosity)
1082
0
{
1083
0
    struct ds f = DS_EMPTY_INITIALIZER;
1084
0
    uint32_t w = ntohl(om->wildcards);
1085
0
    bool skip_type = false;
1086
0
    bool skip_proto = false;
1087
1088
0
    if (!(w & OFPFW10_DL_TYPE)) {
1089
0
        skip_type = true;
1090
0
        if (om->dl_type == htons(ETH_TYPE_IP)) {
1091
0
            if (!(w & OFPFW10_NW_PROTO)) {
1092
0
                skip_proto = true;
1093
0
                if (om->nw_proto == IPPROTO_ICMP) {
1094
0
                    ds_put_cstr(&f, "icmp,");
1095
0
                } else if (om->nw_proto == IPPROTO_TCP) {
1096
0
                    ds_put_cstr(&f, "tcp,");
1097
0
                } else if (om->nw_proto == IPPROTO_UDP) {
1098
0
                    ds_put_cstr(&f, "udp,");
1099
0
                } else if (om->nw_proto == IPPROTO_SCTP) {
1100
0
                    ds_put_cstr(&f, "sctp,");
1101
0
                } else {
1102
0
                    ds_put_cstr(&f, "ip,");
1103
0
                    skip_proto = false;
1104
0
                }
1105
0
            } else {
1106
0
                ds_put_cstr(&f, "ip,");
1107
0
            }
1108
0
        } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
1109
0
            ds_put_cstr(&f, "arp,");
1110
0
        } else if (om->dl_type == htons(ETH_TYPE_RARP)){
1111
0
            ds_put_cstr(&f, "rarp,");
1112
0
        } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
1113
0
            ds_put_cstr(&f, "mpls,");
1114
0
        } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1115
0
            ds_put_cstr(&f, "mplsm,");
1116
0
        } else {
1117
0
            skip_type = false;
1118
0
        }
1119
0
    }
1120
0
    print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
1121
0
                    u16_to_ofp(ntohs(om->in_port)), port_map);
1122
0
    print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
1123
0
               "%d", ntohs(om->dl_vlan));
1124
0
    print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
1125
0
               "%d", om->dl_vlan_pcp);
1126
0
    print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
1127
0
               ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
1128
0
    print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
1129
0
               ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
1130
0
    if (!skip_type) {
1131
0
        print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
1132
0
                   "0x%04x", ntohs(om->dl_type));
1133
0
    }
1134
0
    print_ip_netmask(&f, "nw_src=", om->nw_src,
1135
0
                     (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
1136
0
                     verbosity);
1137
0
    print_ip_netmask(&f, "nw_dst=", om->nw_dst,
1138
0
                     (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
1139
0
                     verbosity);
1140
0
    if (!skip_proto) {
1141
0
        if (om->dl_type == htons(ETH_TYPE_ARP) ||
1142
0
            om->dl_type == htons(ETH_TYPE_RARP)) {
1143
0
            print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
1144
0
                       "%u", om->nw_proto);
1145
0
        } else {
1146
0
            print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
1147
0
                       "%u", om->nw_proto);
1148
0
        }
1149
0
    }
1150
0
    print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
1151
0
               "%u", om->nw_tos);
1152
0
    if (om->nw_proto == IPPROTO_ICMP) {
1153
0
        print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
1154
0
                   "%d", ntohs(om->tp_src));
1155
0
        print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
1156
0
                   "%d", ntohs(om->tp_dst));
1157
0
    } else {
1158
0
        print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
1159
0
                   "%d", ntohs(om->tp_src));
1160
0
        print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
1161
                   "%d", ntohs(om->tp_dst));
1162
0
    }
1163
0
    ds_chomp(&f, ',');
1164
0
    return ds_cstr(&f);
1165
0
}
1166