forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofp-util.c
9607 lines (8262 loc) · 304 KB
/
ofp-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, 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 <config.h>
#include "ofp-print.h"
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/icmp6.h>
#include <stdlib.h>
#include "bundle.h"
#include "byte-order.h"
#include "classifier.h"
#include "dynamic-string.h"
#include "learn.h"
#include "meta-flow.h"
#include "multipath.h"
#include "netdev.h"
#include "nx-match.h"
#include "id-pool.h"
#include "ofp-actions.h"
#include "ofp-errors.h"
#include "ofp-msgs.h"
#include "ofp-util.h"
#include "ofpbuf.h"
#include "openflow/netronome-ext.h"
#include "packets.h"
#include "random.h"
#include "tun-metadata.h"
#include "unaligned.h"
#include "type-props.h"
#include "openvswitch/vlog.h"
#include "bitmap.h"
VLOG_DEFINE_THIS_MODULE(ofp_util);
/* Rate limit for OpenFlow message parse errors. These always indicate a bug
* in the peer and so there's not much point in showing a lot of them. */
static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
static enum ofputil_table_eviction ofputil_decode_table_eviction(
ovs_be32 config, enum ofp_version);
static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
enum ofputil_table_eviction,
enum ofp_version);
struct ofp_prop_header {
ovs_be16 type;
ovs_be16 len;
};
struct ofp_prop_experimenter {
ovs_be16 type; /* OFP*_EXPERIMENTER. */
ovs_be16 length; /* Length in bytes of this property. */
ovs_be32 experimenter; /* Experimenter ID which takes the same form as
* in struct ofp_experimenter_header. */
ovs_be32 exp_type; /* Experimenter defined. */
};
/* Pulls a property, beginning with struct ofp_prop_header, from the beginning
* of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
* nonnull, the entire property, including the header, in '*property'. Returns
* 0 if successful, otherwise an error code.
*
* This function pulls the property's stated size padded out to a multiple of
* 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
* you can use ofputil_pull_property() for that case. */
static enum ofperr
ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property,
unsigned int alignment, uint16_t *typep)
{
struct ofp_prop_header *oph;
unsigned int padded_len;
unsigned int len;
if (msg->size < sizeof *oph) {
return OFPERR_OFPBPC_BAD_LEN;
}
oph = msg->data;
len = ntohs(oph->len);
padded_len = ROUND_UP(len, alignment);
if (len < sizeof *oph || padded_len > msg->size) {
return OFPERR_OFPBPC_BAD_LEN;
}
*typep = ntohs(oph->type);
if (property) {
ofpbuf_use_const(property, msg->data, len);
}
ofpbuf_pull(msg, padded_len);
return 0;
}
/* Pulls a property, beginning with struct ofp_prop_header, from the beginning
* of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
* nonnull, the entire property, including the header, in '*property'. Returns
* 0 if successful, otherwise an error code.
*
* This function pulls the property's stated size padded out to a multiple of
* 8 bytes, which is the common case for OpenFlow properties. */
static enum ofperr
ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
uint16_t *typep)
{
return ofputil_pull_property__(msg, property, 8, typep);
}
static void OVS_PRINTF_FORMAT(2, 3)
log_property(bool loose, const char *message, ...)
{
enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
va_list args;
va_start(args, message);
vlog_valist(THIS_MODULE, level, message, args);
va_end(args);
}
}
static size_t
start_property(struct ofpbuf *msg, uint16_t type)
{
size_t start_ofs = msg->size;
struct ofp_prop_header *oph;
oph = ofpbuf_put_uninit(msg, sizeof *oph);
oph->type = htons(type);
oph->len = htons(4); /* May be updated later by end_property(). */
return start_ofs;
}
static void
end_property(struct ofpbuf *msg, size_t start_ofs)
{
struct ofp_prop_header *oph;
oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
oph->len = htons(msg->size - start_ofs);
ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
}
static void
put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap)
{
for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
start_property(msg, rightmost_1bit_idx(bitmap));
}
}
/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
* an IP netmask with a 1 in each bit that must match and a 0 in each bit that
* is wildcarded.
*
* The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
* is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
* ..., 32 and higher wildcard the entire field. This is the *opposite* of the
* usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
* wildcarded. */
ovs_be32
ofputil_wcbits_to_netmask(int wcbits)
{
wcbits &= 0x3f;
return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
}
/* Given the IP netmask 'netmask', returns the number of bits of the IP address
* that it wildcards, that is, the number of 0-bits in 'netmask', a number
* between 0 and 32 inclusive.
*
* If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
* still be in the valid range but isn't otherwise meaningful. */
int
ofputil_netmask_to_wcbits(ovs_be32 netmask)
{
return 32 - ip_count_cidr_bits(netmask);
}
/* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
* flow_wildcards in 'wc' for use in struct match. It is the caller's
* responsibility to handle the special case where the flow match's dl_vlan is
* set to OFP_VLAN_NONE. */
void
ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
{
BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
/* Initialize most of wc. */
flow_wildcards_init_catchall(wc);
if (!(ofpfw & OFPFW10_IN_PORT)) {
wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
}
if (!(ofpfw & OFPFW10_NW_TOS)) {
wc->masks.nw_tos |= IP_DSCP_MASK;
}
if (!(ofpfw & OFPFW10_NW_PROTO)) {
wc->masks.nw_proto = UINT8_MAX;
}
wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
>> OFPFW10_NW_SRC_SHIFT);
wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
>> OFPFW10_NW_DST_SHIFT);
if (!(ofpfw & OFPFW10_TP_SRC)) {
wc->masks.tp_src = OVS_BE16_MAX;
}
if (!(ofpfw & OFPFW10_TP_DST)) {
wc->masks.tp_dst = OVS_BE16_MAX;
}
if (!(ofpfw & OFPFW10_DL_SRC)) {
WC_MASK_FIELD(wc, dl_src);
}
if (!(ofpfw & OFPFW10_DL_DST)) {
WC_MASK_FIELD(wc, dl_dst);
}
if (!(ofpfw & OFPFW10_DL_TYPE)) {
wc->masks.dl_type = OVS_BE16_MAX;
}
/* VLAN TCI mask. */
if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
}
if (!(ofpfw & OFPFW10_DL_VLAN)) {
wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
}
}
/* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
void
ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
struct match *match)
{
uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
/* Initialize match->wc. */
memset(&match->flow, 0, sizeof match->flow);
ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
/* Initialize most of match->flow. */
match->flow.nw_src = ofmatch->nw_src;
match->flow.nw_dst = ofmatch->nw_dst;
match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
match->flow.tp_src = ofmatch->tp_src;
match->flow.tp_dst = ofmatch->tp_dst;
match->flow.dl_src = ofmatch->dl_src;
match->flow.dl_dst = ofmatch->dl_dst;
match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
match->flow.nw_proto = ofmatch->nw_proto;
/* Translate VLANs. */
if (!(ofpfw & OFPFW10_DL_VLAN) &&
ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
/* Match only packets without 802.1Q header.
*
* When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
*
* If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
* because we can't have a specific PCP without an 802.1Q header.
* However, older versions of OVS treated this as matching packets
* withut an 802.1Q header, so we do here too. */
match->flow.vlan_tci = htons(0);
match->wc.masks.vlan_tci = htons(0xffff);
} else {
ovs_be16 vid, pcp, tci;
uint16_t hpcp;
vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
pcp = htons(hpcp);
tci = vid | pcp | htons(VLAN_CFI);
match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
}
/* Clean up. */
match_zero_wildcarded_fields(match);
}
/* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
void
ofputil_match_to_ofp10_match(const struct match *match,
struct ofp10_match *ofmatch)
{
const struct flow_wildcards *wc = &match->wc;
uint32_t ofpfw;
/* Figure out most OpenFlow wildcards. */
ofpfw = 0;
if (!wc->masks.in_port.ofp_port) {
ofpfw |= OFPFW10_IN_PORT;
}
if (!wc->masks.dl_type) {
ofpfw |= OFPFW10_DL_TYPE;
}
if (!wc->masks.nw_proto) {
ofpfw |= OFPFW10_NW_PROTO;
}
ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
<< OFPFW10_NW_SRC_SHIFT);
ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
<< OFPFW10_NW_DST_SHIFT);
if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
ofpfw |= OFPFW10_NW_TOS;
}
if (!wc->masks.tp_src) {
ofpfw |= OFPFW10_TP_SRC;
}
if (!wc->masks.tp_dst) {
ofpfw |= OFPFW10_TP_DST;
}
if (eth_addr_is_zero(wc->masks.dl_src)) {
ofpfw |= OFPFW10_DL_SRC;
}
if (eth_addr_is_zero(wc->masks.dl_dst)) {
ofpfw |= OFPFW10_DL_DST;
}
/* Translate VLANs. */
ofmatch->dl_vlan = htons(0);
ofmatch->dl_vlan_pcp = 0;
if (match->wc.masks.vlan_tci == htons(0)) {
ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
} else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
&& !(match->flow.vlan_tci & htons(VLAN_CFI))) {
ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
} else {
if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
ofpfw |= OFPFW10_DL_VLAN;
} else {
ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
}
if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
ofpfw |= OFPFW10_DL_VLAN_PCP;
} else {
ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
}
}
/* Compose most of the match structure. */
ofmatch->wildcards = htonl(ofpfw);
ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
ofmatch->dl_src = match->flow.dl_src;
ofmatch->dl_dst = match->flow.dl_dst;
ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
ofmatch->nw_src = match->flow.nw_src;
ofmatch->nw_dst = match->flow.nw_dst;
ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
ofmatch->nw_proto = match->flow.nw_proto;
ofmatch->tp_src = match->flow.tp_src;
ofmatch->tp_dst = match->flow.tp_dst;
memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
}
enum ofperr
ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
uint16_t *padded_match_len)
{
struct ofp11_match_header *omh = buf->data;
uint16_t match_len;
if (buf->size < sizeof *omh) {
return OFPERR_OFPBMC_BAD_LEN;
}
match_len = ntohs(omh->length);
switch (ntohs(omh->type)) {
case OFPMT_STANDARD: {
struct ofp11_match *om;
if (match_len != sizeof *om || buf->size < sizeof *om) {
return OFPERR_OFPBMC_BAD_LEN;
}
om = ofpbuf_pull(buf, sizeof *om);
if (padded_match_len) {
*padded_match_len = match_len;
}
return ofputil_match_from_ofp11_match(om, match);
}
case OFPMT_OXM:
if (padded_match_len) {
*padded_match_len = ROUND_UP(match_len, 8);
}
return oxm_pull_match(buf, match);
default:
return OFPERR_OFPBMC_BAD_TYPE;
}
}
/* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
* Returns 0 if successful, otherwise an OFPERR_* value. */
enum ofperr
ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
struct match *match)
{
uint16_t wc = ntohl(ofmatch->wildcards);
bool ipv4, arp, rarp;
match_init_catchall(match);
if (!(wc & OFPFW11_IN_PORT)) {
ofp_port_t ofp_port;
enum ofperr error;
error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
if (error) {
return OFPERR_OFPBMC_BAD_VALUE;
}
match_set_in_port(match, ofp_port);
}
match_set_dl_src_masked(match, ofmatch->dl_src,
eth_addr_invert(ofmatch->dl_src_mask));
match_set_dl_dst_masked(match, ofmatch->dl_dst,
eth_addr_invert(ofmatch->dl_dst_mask));
if (!(wc & OFPFW11_DL_VLAN)) {
if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
/* Match only packets without a VLAN tag. */
match->flow.vlan_tci = htons(0);
match->wc.masks.vlan_tci = OVS_BE16_MAX;
} else {
if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
/* Match any packet with a VLAN tag regardless of VID. */
match->flow.vlan_tci = htons(VLAN_CFI);
match->wc.masks.vlan_tci = htons(VLAN_CFI);
} else if (ntohs(ofmatch->dl_vlan) < 4096) {
/* Match only packets with the specified VLAN VID. */
match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
} else {
/* Invalid VID. */
return OFPERR_OFPBMC_BAD_VALUE;
}
if (!(wc & OFPFW11_DL_VLAN_PCP)) {
if (ofmatch->dl_vlan_pcp <= 7) {
match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
<< VLAN_PCP_SHIFT);
match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
} else {
/* Invalid PCP. */
return OFPERR_OFPBMC_BAD_VALUE;
}
}
}
}
if (!(wc & OFPFW11_DL_TYPE)) {
match_set_dl_type(match,
ofputil_dl_type_from_openflow(ofmatch->dl_type));
}
ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
/* Invalid TOS. */
return OFPERR_OFPBMC_BAD_VALUE;
}
match_set_nw_dscp(match, ofmatch->nw_tos);
}
if (ipv4 || arp || rarp) {
if (!(wc & OFPFW11_NW_PROTO)) {
match_set_nw_proto(match, ofmatch->nw_proto);
}
match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
}
#define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
switch (match->flow.nw_proto) {
case IPPROTO_ICMP:
/* "A.2.3 Flow Match Structures" in OF1.1 says:
*
* The tp_src and tp_dst fields will be ignored unless the
* network protocol specified is as TCP, UDP or SCTP.
*
* but I'm pretty sure we should support ICMP too, otherwise
* that's a regression from OF1.0. */
if (!(wc & OFPFW11_TP_SRC)) {
uint16_t icmp_type = ntohs(ofmatch->tp_src);
if (icmp_type < 0x100) {
match_set_icmp_type(match, icmp_type);
} else {
return OFPERR_OFPBMC_BAD_FIELD;
}
}
if (!(wc & OFPFW11_TP_DST)) {
uint16_t icmp_code = ntohs(ofmatch->tp_dst);
if (icmp_code < 0x100) {
match_set_icmp_code(match, icmp_code);
} else {
return OFPERR_OFPBMC_BAD_FIELD;
}
}
break;
case IPPROTO_TCP:
case IPPROTO_UDP:
case IPPROTO_SCTP:
if (!(wc & (OFPFW11_TP_SRC))) {
match_set_tp_src(match, ofmatch->tp_src);
}
if (!(wc & (OFPFW11_TP_DST))) {
match_set_tp_dst(match, ofmatch->tp_dst);
}
break;
default:
/* OF1.1 says explicitly to ignore this. */
break;
}
}
if (eth_type_mpls(match->flow.dl_type)) {
if (!(wc & OFPFW11_MPLS_LABEL)) {
match_set_mpls_label(match, 0, ofmatch->mpls_label);
}
if (!(wc & OFPFW11_MPLS_TC)) {
match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
}
}
match_set_metadata_masked(match, ofmatch->metadata,
~ofmatch->metadata_mask);
return 0;
}
/* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
void
ofputil_match_to_ofp11_match(const struct match *match,
struct ofp11_match *ofmatch)
{
uint32_t wc = 0;
memset(ofmatch, 0, sizeof *ofmatch);
ofmatch->omh.type = htons(OFPMT_STANDARD);
ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
if (!match->wc.masks.in_port.ofp_port) {
wc |= OFPFW11_IN_PORT;
} else {
ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
}
ofmatch->dl_src = match->flow.dl_src;
ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
ofmatch->dl_dst = match->flow.dl_dst;
ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
if (match->wc.masks.vlan_tci == htons(0)) {
wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
} else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
&& !(match->flow.vlan_tci & htons(VLAN_CFI))) {
ofmatch->dl_vlan = htons(OFPVID11_NONE);
wc |= OFPFW11_DL_VLAN_PCP;
} else {
if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
ofmatch->dl_vlan = htons(OFPVID11_ANY);
} else {
ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
}
if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
wc |= OFPFW11_DL_VLAN_PCP;
} else {
ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
}
}
if (!match->wc.masks.dl_type) {
wc |= OFPFW11_DL_TYPE;
} else {
ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
}
if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
wc |= OFPFW11_NW_TOS;
} else {
ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
}
if (!match->wc.masks.nw_proto) {
wc |= OFPFW11_NW_PROTO;
} else {
ofmatch->nw_proto = match->flow.nw_proto;
}
ofmatch->nw_src = match->flow.nw_src;
ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
ofmatch->nw_dst = match->flow.nw_dst;
ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
if (!match->wc.masks.tp_src) {
wc |= OFPFW11_TP_SRC;
} else {
ofmatch->tp_src = match->flow.tp_src;
}
if (!match->wc.masks.tp_dst) {
wc |= OFPFW11_TP_DST;
} else {
ofmatch->tp_dst = match->flow.tp_dst;
}
if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
wc |= OFPFW11_MPLS_LABEL;
} else {
ofmatch->mpls_label = htonl(mpls_lse_to_label(
match->flow.mpls_lse[0]));
}
if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
wc |= OFPFW11_MPLS_TC;
} else {
ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
}
ofmatch->metadata = match->flow.metadata;
ofmatch->metadata_mask = ~match->wc.masks.metadata;
ofmatch->wildcards = htonl(wc);
}
/* Returns the "typical" length of a match for 'protocol', for use in
* estimating space to preallocate. */
int
ofputil_match_typical_len(enum ofputil_protocol protocol)
{
switch (protocol) {
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID:
return sizeof(struct ofp10_match);
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID:
return NXM_TYPICAL_LEN;
case OFPUTIL_P_OF11_STD:
return sizeof(struct ofp11_match);
case OFPUTIL_P_OF12_OXM:
case OFPUTIL_P_OF13_OXM:
case OFPUTIL_P_OF14_OXM:
case OFPUTIL_P_OF15_OXM:
return NXM_TYPICAL_LEN;
default:
OVS_NOT_REACHED();
}
}
/* Appends to 'b' an struct ofp11_match_header followed by a match that
* expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
* data appended out to a multiple of 8. 'protocol' must be one that is usable
* in OpenFlow 1.1 or later.
*
* This function can cause 'b''s data to be reallocated.
*
* Returns the number of bytes appended to 'b', excluding the padding. Never
* returns zero. */
int
ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
enum ofputil_protocol protocol)
{
switch (protocol) {
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID:
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID:
OVS_NOT_REACHED();
case OFPUTIL_P_OF11_STD: {
struct ofp11_match *om;
/* Make sure that no padding is needed. */
BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
om = ofpbuf_put_uninit(b, sizeof *om);
ofputil_match_to_ofp11_match(match, om);
return sizeof *om;
}
case OFPUTIL_P_OF12_OXM:
case OFPUTIL_P_OF13_OXM:
case OFPUTIL_P_OF14_OXM:
case OFPUTIL_P_OF15_OXM:
return oxm_put_match(b, match,
ofputil_protocol_to_ofp_version(protocol));
}
OVS_NOT_REACHED();
}
/* Given a 'dl_type' value in the format used in struct flow, returns the
* corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
* structure. */
ovs_be16
ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
{
return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
: flow_dl_type);
}
/* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
* structure, returns the corresponding 'dl_type' value for use in struct
* flow. */
ovs_be16
ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
{
return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
? htons(FLOW_DL_TYPE_NONE)
: ofp_dl_type);
}
/* Protocols. */
struct proto_abbrev {
enum ofputil_protocol protocol;
const char *name;
};
/* Most users really don't care about some of the differences between
* protocols. These abbreviations help with that. */
static const struct proto_abbrev proto_abbrevs[] = {
{ OFPUTIL_P_ANY, "any" },
{ OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
{ OFPUTIL_P_OF10_NXM_ANY, "NXM" },
{ OFPUTIL_P_ANY_OXM, "OXM" },
};
#define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
enum ofputil_protocol ofputil_flow_dump_protocols[] = {
OFPUTIL_P_OF15_OXM,
OFPUTIL_P_OF14_OXM,
OFPUTIL_P_OF13_OXM,
OFPUTIL_P_OF12_OXM,
OFPUTIL_P_OF11_STD,
OFPUTIL_P_OF10_NXM,
OFPUTIL_P_OF10_STD,
};
size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
/* Returns the set of ofputil_protocols that are supported with the given
* OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
* identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
* if 'version' is not supported or outside the valid range. */
enum ofputil_protocol
ofputil_protocols_from_ofp_version(enum ofp_version version)
{
switch (version) {
case OFP10_VERSION:
return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
case OFP11_VERSION:
return OFPUTIL_P_OF11_STD;
case OFP12_VERSION:
return OFPUTIL_P_OF12_OXM;
case OFP13_VERSION:
return OFPUTIL_P_OF13_OXM;
case OFP14_VERSION:
return OFPUTIL_P_OF14_OXM;
case OFP15_VERSION:
return OFPUTIL_P_OF15_OXM;
default:
return 0;
}
}
/* Returns the ofputil_protocol that is initially in effect on an OpenFlow
* connection that has negotiated the given 'version'. 'version' should
* normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
* 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
* outside the valid range. */
enum ofputil_protocol
ofputil_protocol_from_ofp_version(enum ofp_version version)
{
return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
}
/* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
* etc.) that corresponds to 'protocol'. */
enum ofp_version
ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
{
switch (protocol) {
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID:
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID:
return OFP10_VERSION;
case OFPUTIL_P_OF11_STD:
return OFP11_VERSION;
case OFPUTIL_P_OF12_OXM:
return OFP12_VERSION;
case OFPUTIL_P_OF13_OXM:
return OFP13_VERSION;
case OFPUTIL_P_OF14_OXM:
return OFP14_VERSION;
case OFPUTIL_P_OF15_OXM:
return OFP15_VERSION;
}
OVS_NOT_REACHED();
}
/* Returns a bitmap of OpenFlow versions that are supported by at
* least one of the 'protocols'. */
uint32_t
ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
{
uint32_t bitmap = 0;
for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
enum ofputil_protocol protocol = rightmost_1bit(protocols);
bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
}
return bitmap;
}
/* Returns the set of protocols that are supported on top of the
* OpenFlow versions included in 'bitmap'. */
enum ofputil_protocol
ofputil_protocols_from_version_bitmap(uint32_t bitmap)
{
enum ofputil_protocol protocols = 0;
for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
enum ofp_version version = rightmost_1bit_idx(bitmap);
protocols |= ofputil_protocols_from_ofp_version(version);
}
return protocols;
}
/* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
* otherwise. */
bool
ofputil_protocol_is_valid(enum ofputil_protocol protocol)
{
return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
}
/* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
* extension turned on or off if 'enable' is true or false, respectively.
*
* This extension is only useful for protocols whose "standard" version does
* not allow specific tables to be modified. In particular, this is true of
* OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
* specifies a table ID and so there is no need for such an extension. When
* 'protocol' is such a protocol that doesn't need a flow_mod_table_id
* extension, this function just returns its 'protocol' argument unchanged
* regardless of the value of 'enable'. */
enum ofputil_protocol
ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
{
switch (protocol) {
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID:
return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID:
return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
case OFPUTIL_P_OF11_STD:
return OFPUTIL_P_OF11_STD;
case OFPUTIL_P_OF12_OXM:
return OFPUTIL_P_OF12_OXM;
case OFPUTIL_P_OF13_OXM:
return OFPUTIL_P_OF13_OXM;
case OFPUTIL_P_OF14_OXM:
return OFPUTIL_P_OF14_OXM;
case OFPUTIL_P_OF15_OXM:
return OFPUTIL_P_OF15_OXM;
default:
OVS_NOT_REACHED();
}
}
/* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
* some extension to a standard protocol version, the return value is the
* standard version of that protocol without any extension. If 'protocol' is a
* standard protocol version, returns 'protocol' unchanged. */
enum ofputil_protocol
ofputil_protocol_to_base(enum ofputil_protocol protocol)
{
return ofputil_protocol_set_tid(protocol, false);
}
/* Returns 'new_base' with any extensions taken from 'cur'. */
enum ofputil_protocol
ofputil_protocol_set_base(enum ofputil_protocol cur,
enum ofputil_protocol new_base)
{
bool tid = (cur & OFPUTIL_P_TID) != 0;
switch (new_base) {
case OFPUTIL_P_OF10_STD:
case OFPUTIL_P_OF10_STD_TID:
return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
case OFPUTIL_P_OF10_NXM:
case OFPUTIL_P_OF10_NXM_TID:
return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
case OFPUTIL_P_OF11_STD:
return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
case OFPUTIL_P_OF12_OXM:
return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
case OFPUTIL_P_OF13_OXM:
return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
case OFPUTIL_P_OF14_OXM:
return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
case OFPUTIL_P_OF15_OXM:
return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
default:
OVS_NOT_REACHED();
}
}
/* Returns a string form of 'protocol', if a simple form exists (that is, if
* 'protocol' is either a single protocol or it is a combination of protocols
* that have a single abbreviation). Otherwise, returns NULL. */
const char *
ofputil_protocol_to_string(enum ofputil_protocol protocol)
{
const struct proto_abbrev *p;
/* Use a "switch" statement for single-bit names so that we get a compiler
* warning if we forget any. */
switch (protocol) {
case OFPUTIL_P_OF10_NXM:
return "NXM-table_id";
case OFPUTIL_P_OF10_NXM_TID:
return "NXM+table_id";
case OFPUTIL_P_OF10_STD:
return "OpenFlow10-table_id";
case OFPUTIL_P_OF10_STD_TID:
return "OpenFlow10+table_id";
case OFPUTIL_P_OF11_STD:
return "OpenFlow11";
case OFPUTIL_P_OF12_OXM:
return "OXM-OpenFlow12";
case OFPUTIL_P_OF13_OXM:
return "OXM-OpenFlow13";
case OFPUTIL_P_OF14_OXM:
return "OXM-OpenFlow14";