-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdpif-netdev-nmu.c
More file actions
3462 lines (2905 loc) · 101 KB
/
dpif-netdev-nmu.c
File metadata and controls
3462 lines (2905 loc) · 101 KB
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
#include <byteswap.h>
#include <config.h>
#include <float.h>
#ifdef HAVE_NUEVOMATCHUP
#include <libnuevomatchup.h>
#endif
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "bitmap.h"
#include "byte-order.h"
#include "cmap.h"
#include "dpif.h"
#include "dpif-netdev-nmu.h"
#include "dpif-netdev-perf.h"
#include "dpif-netdev-private.h"
#include "flow.h"
#include "hash.h"
#include "odp-util.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/list.h"
#include "openvswitch/match.h"
#include "openvswitch/thread.h"
#include "openvswitch/vlog.h"
#include "openvswitch/util.h"
#include "ovs-atomic.h"
#include "ovs-thread.h"
#include "smap.h"
#include "util.h"
#include "uuid.h"
/* Ignore unused warnings in GCC when libnuevomatchup is missing */
#ifndef __always_unused
#define __always_unused __attribute__((unused))
#endif
/* Interval between adjacent cmpflow synchronization with slow path (ms) */
#define CMPFLOW_SYNC_INTERVAL 10
/* Converts integers from network (big-endian) to host. */
#define NMUCLS_NTHBE_8(X) X
#define NMUCLS_NTHBE_16(X) ntohs(X)
#define NMUCLS_NTHBE_32(X) ntohl(X)
/* Bit masks */
#define NMUCLS_BITMASK_8 0xff
#define NMUCLS_BITMASK_16 0xffff
#define NMUCLS_BITMASK_32 0xffffffff
/* Helpers to measure time */
#define TIMESPAN_GET_NS(DEST, START, END) \
DEST=-(START.tv_sec * 1e9 + START.tv_nsec) \
+(END.tv_sec * 1e9 + END.tv_nsec);
#define TIMESPAN_MEASURE(NAME) \
clock_gettime(CLOCK_MONOTONIC, &NAME);
/* Extract 'FIELD' from 'FLOW_P' and 'WILDCARD_P', then populate 'FIELD'
* of 'CMPFLOW' with values of 'SIZE' bits */
#define NMUCLS_EXTRACT_FIELD(FIELD, FLOW_P, WILDCARD_P, CMPFLOW, IDX, SIZE) \
do { \
uint##SIZE##_t mask = NMUCLS_NTHBE_##SIZE(WILDCARD_P->masks.FIELD); \
CMPFLOW.fields[IDX].low = NMUCLS_NTHBE_##SIZE(FLOW_P->FIELD) & mask; \
CMPFLOW.fields[IDX].high = (CMPFLOW.fields[IDX].low | ~mask) & \
NMUCLS_BITMASK_##SIZE; \
} while (0);
VLOG_DEFINE_THIS_MODULE(dpif_netdev_nmu);
struct nmu_trainer;
struct rule_info;
struct nmu_config {
const char *cmpflow_bridge_name;
int cool_down_time_ms;
int garbage_collection_ms;
int error_threshold;
int max_collision;
int minimal_coverage;
int train_threshold;
int max_retrain_sessions;
int samples_per_session;
bool nmu_enable;
bool use_batching;
bool use_cmpflows;
bool instant_remainder;
};
struct nmu_statistics {
double hit_count;
double hitrate;
double total_packets;
double parsing_ns;
double remainder_ns;
double inference_ns;
double search_ns;
double validation_ns;
double avg_matches;
double num_validations;
};
struct nmucls {
struct nmu_config *cfg;
struct dp_netdev_pmd_thread *pmd;
struct nmu_trainer *nmt;
struct cmap cmpflow_table;
long long int cmpflow_sync_time;
long long int garbage_time;
bool thread_running;
pthread_t manager_thread;
atomic_bool enabled;
};
/* Static method declaration */
#ifdef HAVE_NUEVOMATCHUP
typedef uint64_t
(*rule_match_func)(const struct lnmu_iset_match* iset_match,
const struct netdev_flow_key *key);
enum rule_type {
REMAINDER_TO_ISET = 0,
ISET_TO_REMAINDER = 1,
DELETED_RULES = 2
};
static inline struct nmucls *
nmucls_init__(struct nmu_config __always_unused *cfg,
struct dp_netdev_pmd_thread __always_unused *pmd);
static inline void nmucls_run__(struct nmucls *nmucls);
static inline void
nmucls_insert__(struct nmucls *nmucls,
const struct flow*,
struct flow_wildcards*,
struct dpcls_rule *rule,
struct cmpflow_iterator *it);
static inline int
nmucls_remove__(struct nmucls *nmucls,
struct dpcls *cls,
struct dp_netdev_flow *flow);
static inline void
nmucls_print_rule_with_key__(const struct nmucls *nmucls,
const struct netdev_flow_key *key);
static void *nmucls_thread_main(void*);
static void nmu_move_rules_from_remainder(struct nmucls *nmucls);
static void nmu_move_rules_from_isets(struct nmucls *nmucls);
static inline enum lnmu_inclusion_policy
nmucls_set_inclusion_policy(struct nmucls *nmucls);
static void nmu_unref_deleted_flows(struct nmucls *nmucls);
static void nmu_insert(struct nmucls *,
const struct flow *,
const struct flow_wildcards *,
struct dpcls_rule *,
struct cmpflow *);
static void nmu_remove(struct nmucls *, struct dpcls_rule *rule);
static inline void nmu_lookup(struct nmucls *,
const struct netdev_flow_key **keys,
uint32_t *keys_map,
const int count,
struct dpcls_rule **results);
static void nmu_init(struct nmucls *);
static void nmu_destroy(struct nmucls *);
static inline void nmu_print_stats(struct ds *reply,
struct nmucls *nmucls,
const char *sep);
static inline void nmu_clear_stats(struct nmucls *nmucls);
static void nmu_rule_get_status(struct nmucls *,
const struct dpcls_rule *rule,
bool *in_nmu,
bool *removed,
uint32_t *lib_id);
static inline void nmu_debug_print(const struct nmucls *nmucls,
const struct netdev_flow_key *key,
struct dpcls_rule **result);
static inline uint32_t min_uint(uint32_t a, uint32_t b);
static inline uint32_t max_uint(uint32_t a, uint32_t b);
static inline uint32_t max_int(int a, int b);
static int iset_entry_precedence_compare(const void *a, const void *b);
static inline struct iset_match_impl *
iset_match_get_impl(const struct lnmu_iset_match *);
static inline struct iset_impl * iset_get_impl(const struct lnmu_iset *iset);
static void ptr_list_destroy(struct ovs_list *lst);
static void ptr_list_push_ptr(struct ovs_list *lst, void *ptr);
static void free_list_destroy(struct ovs_list *lst);
static inline void rule_info_unlock(struct rule_info *info);
static inline void rule_info_lock(struct rule_info *info);
static struct rule_info* rule_info_lookup(const struct nmucls *,
const struct dpcls_rule *);
static void rule_to_string(const struct nmucls *,
struct rule_info *,
struct ds *);
static void nmu_rule_lock(struct nmucls *, const struct dpcls_rule *);
static void nmu_rule_unlock(struct nmucls *, const struct dpcls_rule *);
static int nmu_classifier_remove(struct lnmu_nuevomatchup *nuevomatch,
const struct rule_info *rule_info);
static void nmu_preprocess_rules(struct nmucls *nmucls);
static void nmu_postprocess_rules(struct nmucls *nmucls);
static void nmu_garbage_collect_rules(struct nmucls *nmucls);
static void nmu_train(struct nmucls *nmucls, bool *new_classifier);
static void nmu_get_rules(struct nmucls *,
enum rule_type type,
struct dpcls_rule ***rules,
size_t* size);
static void nmu_switch(struct nmucls *nmucls);
static size_t nmu_wait(struct nmucls *nmucls);
static inline bool nmu_cmpflow_enabled(const struct nmucls *);
static inline void ALWAYS_INLINE
nmu_extract_flow_fields(const struct netdev_flow_key** keys,
const size_t count,
uint32_t *header_p);
static struct cmpflow_item* cmpflow_lookup(struct nmucls *, uint64_t id);
static void trainer_thread_ref(void);
static int trainer_thread_unref(void);
static bool trainer_thread_produce(struct nmucls *nmucls);
#endif
bool
nmucls_enabled(struct nmucls *nmucls)
{
return nmucls && nmucls->enabled;
}
bool
nmu_config_enabled(struct nmu_config *cfg)
{
return cfg && cfg->nmu_enable;
}
bool
nmucls_cmpflow_enabled(struct nmucls *nmucls)
{
#ifndef HAVE_NUEVOMATCHUP
return false;
#else
return nmucls_enabled(nmucls) && nmu_cmpflow_enabled(nmucls);
#endif
}
struct nmu_config *
nmu_config_init(void)
{
struct nmu_config *cfg;
cfg = xmalloc(sizeof(*cfg));
memset(cfg, 0, sizeof(*cfg));
return cfg;
}
void
nmu_config_destroy(struct nmu_config *cfg)
{
free(cfg);
}
int
nmu_config_read(struct nmu_config *cfg,
const struct smap *other_config)
{
bool old_state;
atomic_read_relaxed(&cfg->nmu_enable, &old_state);
cfg->nmu_enable = smap_get_bool(other_config, "nmu-enable", false);
#ifndef HAVE_NUEVOMATCHUP
cfg->nmu_enable = false;
#endif
if (!cfg->nmu_enable) {
VLOG_INFO("NuevoMatchUp is disabled");
goto exit;
}
cfg->cool_down_time_ms =
smap_get_int(other_config, "nmu-cool-down-time-ms", 2000);
cfg->error_threshold =
smap_get_int(other_config, "nmu-error-threshold", 128);
cfg->max_collision = smap_get_int(other_config, "nmu-max-collision", 16);
cfg->minimal_coverage =
smap_get_int(other_config, "num-minimal-coverage", 25);
cfg->train_threshold =
smap_get_int(other_config, "nmu-train-threshold", 90);
cfg->garbage_collection_ms =
smap_get_int(other_config, "nmu-garbage-collection-ms", 0);
cfg->max_retrain_sessions =
(uint8_t)smap_get_int(other_config, "nmu-sessions", 6);
cfg->samples_per_session =
(uint16_t)smap_get_int(other_config, "nmu-samples", 4000);
cfg->use_batching =
smap_get_bool(other_config, "nmu-batching", false);
cfg->use_cmpflows =
smap_get_bool(other_config, "nmu-use-cmpflows", false);
cfg->cmpflow_bridge_name =
smap_get_def(other_config, "nmu-cmpflows-bridge-name", "");
cfg->instant_remainder =
smap_get_bool(other_config, "nmu-instant-remainder", false);
VLOG_INFO("NuevoMatchUp is enabled with the following:"
"max-collision: %d, error-threshold: %d, "
"min-coverage: %d, cool-down: %d, "
"train-threshold: %d, "
"garbage-collection: %d, allow-cmpflows: %d, "
"instant-remainder: %d, "
"sessions: %d, samples: %d, batching: %d ",
cfg->max_collision,
cfg->error_threshold,
cfg->minimal_coverage,
cfg->cool_down_time_ms,
cfg->train_threshold,
cfg->garbage_collection_ms,
cfg->use_cmpflows,
cfg->instant_remainder,
cfg->max_retrain_sessions,
cfg->samples_per_session,
cfg->use_batching);
exit:
return old_state != cfg->nmu_enable;
}
struct nmucls *
nmucls_init(struct nmu_config __always_unused *cfg,
struct dp_netdev_pmd_thread __always_unused *pmd)
{
#ifdef HAVE_NUEVOMATCHUP
return nmucls_init__(cfg, pmd);
#else
return NULL;
#endif
}
void
nmucls_run(struct nmucls *nmucls)
{
#ifdef HAVE_NUEVOMATCHUP
nmucls_run__(nmucls);
#endif
}
void
nmucls_destroy(struct nmucls *nmucls)
{
if (!nmucls_enabled(nmucls)) {
return;
}
#ifdef HAVE_NUEVOMATCHUP
atomic_store(&nmucls->enabled, false);
xpthread_join(nmucls->manager_thread, NULL);
nmu_destroy(nmucls);
cmap_destroy(&nmucls->cmpflow_table);
free(nmucls->nmt);
free(nmucls);
#endif
}
void
nmucls_insert(struct nmucls __always_unused *nmucls,
const struct flow __always_unused *flow,
struct flow_wildcards __always_unused *wc,
struct dpcls_rule __always_unused *rule,
struct cmpflow_iterator __always_unused *it)
{
#ifdef HAVE_NUEVOMATCHUP
nmucls_insert__(nmucls, flow, wc, rule, it);
#endif
}
int
nmucls_remove(struct nmucls __always_unused *nmucls,
struct dpcls __always_unused *cls,
struct dp_netdev_flow __always_unused *flow)
{
#ifdef HAVE_NUEVOMATCHUP
return nmucls_remove__(nmucls, cls, flow);
#else
return 0;
#endif
}
void nmucls_lookup(struct nmucls __always_unused *nmucls,
const struct netdev_flow_key __always_unused **keys,
uint32_t __always_unused *keys_map,
size_t __always_unused cnt,
struct dpcls_rule __always_unused **rules)
{
#ifdef HAVE_NUEVOMATCHUP
return nmu_lookup(nmucls, keys, keys_map, cnt, rules);
#endif
}
void
nmucls_print_rule_with_key(const struct nmucls __always_unused *nmucls,
const struct netdev_flow_key __always_unused *key)
{
#ifdef HAVE_NUEVOMATCHUP
nmucls_print_rule_with_key__(nmucls, key);
#endif
}
void
nmucls_print_stats(struct ds *reply,
struct nmucls *nmucls,
const char *sep)
{
#ifdef HAVE_NUEVOMATCHUP
nmu_print_stats(reply, nmucls, sep);
#endif
}
void
nmucls_clear_stats(struct nmucls __always_unused *nmucls)
{
#ifdef HAVE_NUEVOMATCHUP
nmu_clear_stats(nmucls);
#endif
}
bool
nmucls_rule_is_cmpflow(const struct dpcls_rule *rule)
{
return rule->is_cmpflow;
}
void
nmucls_rule_lock(struct nmucls __always_unused *nmucls,
struct dpcls_rule __always_unused *dpcls_rule)
{
#ifdef HAVE_NUEVOMATCHUP
nmu_rule_lock(nmucls, dpcls_rule);
#endif
}
void
nmucls_rule_unlock(struct nmucls __always_unused *nmucls,
struct dpcls_rule __always_unused *dpcls_rule)
{
#ifdef HAVE_NUEVOMATCHUP
nmu_rule_unlock(nmucls, dpcls_rule);
#endif
}
/* OVS - NuevoMatchUp internals */
#ifdef HAVE_NUEVOMATCHUP
/* Additional information for iSet rules */
struct iset_match_impl {
void *match_func; /* Match function */
void *mask;
void *flow;
uint64_t *mf_masks;
const uint64_t *mf_values;
int priority;
uint8_t mf_bits_set_unit0;
uint8_t mf_bits_set_unit1;
};
struct nmu_trainer {
struct nmucls *nmucls; /* Parent struct */
struct nmu_statistics stats; /* Statistics */
struct cmap rule_map; /* holds all rules */
struct ovs_spin rule_map_lock; /* guards "rule_map" */
struct ovs_spin remainder_lock; /* prevents concurrent reads/writes */
struct timespec last_update; /* for measuring time between updates */
struct classifier_version *active; /* active classifier */
struct classifier_version *shadow; /* classifier in training */
struct classifier_version *obsolete; /* destroyed in the next switch */
int shadow_valid; /* whether "shadow->nm" is valid */
int version; /* version of active classifier */
bool updates; /* updates since last training */
size_t size; /* size of nuevomatchup in bytes */
bool instant_remainder; /* instant remainder insertions w/ cmpflows */
size_t num_of_isets;
double triaing_time_ns; /* last training time */
double cool_down_time_ms; /* time between adjacent training */
double train_threshold; /* minimal coverage for training */
size_t num_of_remainder_rules; /* used for measuring coverage */
size_t num_of_iset_rules; /* used for measuring coverage */
size_t num_of_rules_inserted; /* used for measuring coverage */
size_t num_of_iset_rules_out; /* used for measuring coverage */
size_t num_of_total_rules_out; /* used for measuring coverage */
struct ovs_list free_list; /* rules in "rule_map" can be in either */
struct ovs_list rem_to_iset; /* one of these lists, mutually exclusive */
struct ovs_list iset_to_rem; /* these lists are used to update dpcls */
struct ovs_list deleted_rules; /* and to perform garbage collection */
struct lnmu_trainer trainer; /* libnuevomatchup trainer */
};
/* Additional private information of iSets */
struct iset_impl {
struct cmap map;
int valid;
bool remainder_enabled;
};
/* Reflection on iSet rules */
struct iset_rule_info {
struct cmap_node node; /* Within "iset_impl"*/
int entry_idx; /* Fast access to iSet entry */
void *rule_p; /* Pointer to dpcls rule */
struct rule_info *rule_info; /* General information on this rule */
uint32_t hash;
};
/* Used for sorting iSet entry rules by priority */
struct iset_entry_precedence {
int index;
int priority;
};
/* Linked list of pointers */
struct ptr_list {
struct ovs_list node; /* Within "ovs_list" */
void *ptr;
};
/* Used for fast switching between classifier versions using pointers */
struct classifier_version {
struct lnmu_nuevomatchup *nmu; /* NMU classifier */
void *rem; /* Remainder, using cmpflows */
};
/* Necessary information and status on rules and flows */
struct rule_info {
struct cmap_node node; /* Within nmu_trainer */
struct ovs_spin lock; /* Prevent multiple writers */
struct cmpflow *cmpflow; /* Pointer to cmpflow from netdev */
void *rule_p; /* Pointer to unique dpcls rule */
bool in_lib; /* Held by libnuevomatchup */
bool removed; /* Removed by a revalidator */
bool delete_me; /* Marked for garbage collection */
enum lnmu_inclusion_policy flags; /* Allow in iSet/Remainder/Both */
uint32_t hash; /* Hash for "rule_map" */
uint32_t lib_unique_id; /* Unique id in libnuevomatchup */
int version; /* Classifier version */
int subset_idx; /* iSet index or -1 for remainder */
struct lnmu_flow flow; /* 5-tuple flow for libnuevomatchup */
};
/* Items in nmucls->cmpflow_table */
struct cmpflow_item {
struct cmap_node node;
struct dp_netdev_flow *flow;
};
static struct vlog_rate_limit cmpflow_rl = VLOG_RATE_LIMIT_INIT(600, 600);
static inline uint32_t
min_uint(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
static inline uint32_t
max_uint(uint32_t a, uint32_t b)
{
return a > b ? a : b;
}
static inline uint32_t
max_int(int a, int b)
{
return a > b ? a : b;
}
static int
iset_entry_precedence_compare(const void *a, const void *b)
{
struct iset_entry_precedence *first, *second;
first = (struct iset_entry_precedence*)a;
second = (struct iset_entry_precedence*)b;
if (first->priority > second->priority) {
return -1;
} else if (first->priority < second->priority) {
return 1;
} else {
return 0;
}
}
static void
ptr_list_destroy(struct ovs_list *lst)
{
struct ptr_list *item;
LIST_FOR_EACH_POP(item, node, lst) {
ovsrcu_postpone(free, item);
}
ovs_list_init(lst);
}
static void
ptr_list_push_ptr(struct ovs_list *lst, void *ptr)
{
struct ptr_list *item;
item = xmalloc(sizeof(struct ptr_list));
item->ptr = ptr;
ovs_list_push_back(lst, &item->node);
}
static void
free_list_destroy(struct ovs_list *lst)
{
struct ptr_list *item;
LIST_FOR_EACH_POP(item, node, lst) {
ovsrcu_postpone(free, (char*)item->ptr);
}
ptr_list_destroy(lst);
}
static inline void
netdev_flow_key_flatten_unit(const uint64_t *pkt_blocks,
const uint64_t *tbl_blocks,
const uint64_t *mf_masks,
uint64_t *blocks_scratch,
const uint64_t pkt_mf_bits,
const uint32_t count)
{
uint32_t i;
for (i = 0; i < count; i++) {
uint64_t mf_mask = mf_masks[i];
/* Calculate the block index for the packet metadata. */
uint64_t idx_bits = mf_mask & pkt_mf_bits;
const uint32_t pkt_idx = count_1bits(idx_bits);
/* Check if the packet has the subtable miniflow bit set. If yes, the
* block at the above pkt_idx will be stored, otherwise it is masked
* out to be zero.
*/
uint64_t pkt_has_mf_bit = (mf_mask + 1) & pkt_mf_bits;
uint64_t no_bit = ((!pkt_has_mf_bit) > 0) - 1;
/* Mask packet block by table block, and mask to zero if packet
* doesn't actually contain this block of metadata.
*/
blocks_scratch[i] = pkt_blocks[pkt_idx] & tbl_blocks[i] & no_bit;
}
}
/* This function takes a packet, and subtable and writes an array of uint64_t
* blocks. The blocks contain the metadata that the subtable matches on, in
* the same order as the subtable, allowing linear iteration over the blocks.
*
* To calculate the blocks contents, the netdev_flow_key_flatten_unit function
* is called twice, once for each "unit" of the miniflow. This call can be
* inlined by the compiler for performance.
*
* Note that the u0_count and u1_count variables can be compile-time constants,
* allowing the loop in the inlined flatten_unit() function to be compile-time
* unrolled, or possibly removed totally by unrolling by the loop iterations.
* The compile time optimizations enabled by this design improves performance.
*/
static inline void
netdev_flow_key_flatten(const struct netdev_flow_key *key,
const struct netdev_flow_key *mask,
const uint64_t *mf_masks,
uint64_t *blocks_scratch,
const uint32_t u0_count,
const uint32_t u1_count)
{
/* Load mask from subtable, mask with packet mf, popcount to get idx. */
const uint64_t *pkt_blocks = miniflow_get_values(&key->mf);
const uint64_t *tbl_blocks = miniflow_get_values(&mask->mf);
/* Packet miniflow bits to be masked by pre-calculated mf_masks. */
const uint64_t pkt_bits_u0 = key->mf.map.bits[0];
const uint32_t pkt_bits_u0_pop = count_1bits(pkt_bits_u0);
const uint64_t pkt_bits_u1 = key->mf.map.bits[1];
/* Unit 0 flattening */
netdev_flow_key_flatten_unit(&pkt_blocks[0],
&tbl_blocks[0],
&mf_masks[0],
&blocks_scratch[0],
pkt_bits_u0,
u0_count);
/* Unit 1 flattening:
* Move the pointers forward in the arrays based on u0 offsets, NOTE:
* 1) pkt blocks indexed by actual popcount of u0, which is NOT always
* the same as the amount of bits set in the subtable.
* 2) mf_masks, tbl_block and blocks_scratch are all "flat" arrays, so
* the index is always u0_count.
*/
netdev_flow_key_flatten_unit(&pkt_blocks[pkt_bits_u0_pop],
&tbl_blocks[u0_count],
&mf_masks[u0_count],
&blocks_scratch[u0_count],
pkt_bits_u1,
u1_count);
}
static inline uint64_t ALWAYS_INLINE
rule_match_impl(const struct lnmu_iset_match* iset_match,
const struct netdev_flow_key *key,
const uint32_t bit_count_u0,
const uint32_t bit_count_u1)
{
if (!iset_match->match) {
return 0;
}
struct iset_match_impl *impl = iset_match_get_impl(iset_match);
uint32_t bit_count_total = bit_count_u0 + bit_count_u1;
struct netdev_flow_key *mask;
uint64_t blocks_scratch[bit_count_total];
uint64_t not_match;
not_match = 0;
mask = (struct netdev_flow_key*)impl->mask;
netdev_flow_key_flatten(key,
mask,
impl->mf_masks,
blocks_scratch,
bit_count_u0,
bit_count_u1);
const uint64_t *keyp = impl->mf_values;
const uint64_t *maskp = miniflow_get_values(&mask->mf);
for (int i = 0; i < bit_count_total; i++) {
not_match |= (blocks_scratch[i] & maskp[i]) != keyp[i];
}
/* Invert result to show match as 1. */
return !not_match;
}
static uint64_t
rule_match_generic(const struct lnmu_iset_match* iset_match,
const struct netdev_flow_key *key)
{
struct iset_match_impl *impl = iset_match_get_impl(iset_match);
return rule_match_impl(iset_match,
key,
impl->mf_bits_set_unit0,
impl->mf_bits_set_unit1);
}
/* Expand out specialized functions with U0 and U1 bit attributes. */
#define DECLARE_OPTIMIZED_LOOKUP_FUNCTION(U0, U1) \
static uint64_t \
rule_match__mf_u0w##U0##_u1w##U1( \
const struct lnmu_iset_match *iset_match, \
const struct netdev_flow_key *key) \
{ \
return rule_match_impl(iset_match, key, U0, U1); \
} \
DECLARE_OPTIMIZED_LOOKUP_FUNCTION(5, 1);
DECLARE_OPTIMIZED_LOOKUP_FUNCTION(4, 1);
DECLARE_OPTIMIZED_LOOKUP_FUNCTION(4, 0);
/* Check if a specialized function is valid for the required subtable. */
#define CHECK_LOOKUP_FUNCTION(U0, U1) \
if (!f && u0_bits == U0 && u1_bits == U1) { \
f = rule_match__mf_u0w##U0##_u1w##U1; \
}
static rule_match_func
rule_match_probe(uint8_t u0_bits, uint8_t u1_bits)
{
rule_match_func f = NULL;
CHECK_LOOKUP_FUNCTION(5, 1);
CHECK_LOOKUP_FUNCTION(4, 1);
CHECK_LOOKUP_FUNCTION(4, 0);
if (!f) {
f = rule_match_generic;
}
return f;
}
static inline struct iset_match_impl *
iset_match_get_impl(const struct lnmu_iset_match *iset_match)
{
return CONST_CAST(struct iset_match_impl*, iset_match->data);
}
static inline struct iset_impl *
iset_get_impl(const struct lnmu_iset *iset)
{
return CONST_CAST(struct iset_impl*, iset->args);
}
static void
iset_match_invalidate(struct lnmu_iset *iset, int entry_idx, int rule_idx)
{
struct lnmu_iset_match *iset_match;
size_t db_entry, val_entry;
uint32_t *val_matrix;
int *entry_rules;
int offset;
db_entry = iset->row_size * entry_idx;
val_entry = db_entry*2*LNMU_FIELD_NUM;
iset_match = &iset->match_db[db_entry];
entry_rules = &iset->entry_rules[entry_idx];
val_matrix = &iset->validation_db[val_entry];
/* Switch the current rule with the last rule */
(*entry_rules)--;
/* Invalidate rule */
iset_match[rule_idx].match = NULL;
iset_match[rule_idx].args = NULL;
free(iset_match[rule_idx].data);
iset_match[rule_idx].data = NULL;
for (int k=0; k<LNMU_FIELD_NUM; ++k) {
offset = k*iset->row_size;
val_matrix[rule_idx+offset] = 0xFFFFFFFF;
val_matrix[rule_idx+offset+iset->hi_values] = 0;
}
}
static void
iset_entry_sort_by_priority(struct lnmu_iset *iset, int entry_idx)
{
struct iset_match_impl *iset_match_impl;
struct iset_entry_precedence *elements;
struct lnmu_iset_match *iset_match, *iset_match_cpy;
uint32_t *val_matrix, *val_matrix_cpy;
size_t db_entry, val_entry;
int idx, offset;
const int mat_rows = iset->row_size;
const int mat_cols = 2*LNMU_FIELD_NUM;
db_entry = mat_rows*entry_idx;
val_entry = mat_rows*mat_cols*entry_idx;
iset_match = &iset->match_db[db_entry];
val_matrix = &iset->validation_db[val_entry];
/* Copy the iset match and the validation matrix */
iset_match_cpy = xmemdup(iset_match, sizeof(*iset_match)*mat_rows);
val_matrix_cpy = xmemdup(val_matrix, sizeof(*val_matrix)*mat_rows*mat_cols);
/* Sort rules by priority (largest to smallest) */
elements = xmalloc(sizeof(*elements)*mat_rows);
for (int i=0; i<iset->row_size; ++i) {
iset_match_impl = iset_match_get_impl(&iset_match[i]);
elements[i].index = i;
elements[i].priority = iset_match_impl ?
iset_match_impl->priority :
-1;
}
qsort(elements, mat_rows, sizeof(*elements),
iset_entry_precedence_compare);
/* Re-order elements from the memory copy */
for (int i=0; i<mat_rows; ++i) {
idx = elements[i].index;
iset_match[i] = iset_match_cpy[idx];
for (int k=0; k<mat_cols; ++k) {
offset = k*mat_rows;
val_matrix[i+offset] = val_matrix_cpy[idx+offset];
}
}
free(elements);
free(iset_match_cpy);
free(val_matrix_cpy);
}
static inline void
iset_init(struct lnmu_iset *iset)
{
struct iset_match_impl *iset_match_impl;
struct iset_rule_info *iset_rule_info;
struct rule_info *rule_info;
struct lnmu_iset_match *iset_match;
struct dpcls_rule *rule_p;
struct cmpflow *cmpflow;
iset->args = xmalloc(sizeof(struct iset_impl));
iset_get_impl(iset)->valid = 1;
cmap_init(&iset_get_impl(iset)->map);
for (size_t i=0; i<iset->num_of_entries; ++i) {
for (int j=0; j<iset->entry_rules[i]; ++j) {
iset_match = &iset->match_db[i*iset->row_size+j];
iset_match->data = NULL;
/* Skip NULL matches */
if (!iset_match->match) {
continue;
}
/* Lock on rule info */
rule_info = (struct rule_info*)iset_match->args;
rule_info_lock(rule_info);
/* Check that the rule was not removed */
if (rule_info->removed) {
iset_match->match = NULL;
free(iset_match->data);
rule_info_unlock(rule_info);
continue;
}
/* Assign iset match additional data */
iset_match_impl = xmalloc(sizeof(struct iset_match_impl));
iset_match->data = iset_match_impl;
/* Set the iSet match */
rule_p = (struct dpcls_rule*)iset_match->match;
cmpflow = rule_info->cmpflow;
iset_match_impl->mf_bits_set_unit0 = cmpflow->mf_bits_set_unit0;
iset_match_impl->mf_bits_set_unit1 = cmpflow->mf_bits_set_unit1;
iset_match_impl->flow = cmpflow->flow;
iset_match_impl->mask = cmpflow->mask;
iset_match_impl->mf_masks = cmpflow->mf_masks;
iset_match_impl->mf_values = cmpflow->mf_values;
iset_match_impl->priority = cmpflow->priority;
iset_match_impl->match_func =
(void*)rule_match_probe(iset_match_impl->mf_bits_set_unit0,
iset_match_impl->mf_bits_set_unit1);
/* Set the iSet match map */
iset_rule_info = xmalloc(sizeof *iset_rule_info);
iset_rule_info->rule_p = rule_p;
iset_rule_info->entry_idx = i;
iset_rule_info->hash = hash_pointer(rule_info, 0);
iset_rule_info->rule_info = rule_info;
cmap_insert(&iset_get_impl(iset)->map, &iset_rule_info->node,
iset_rule_info->hash);
rule_info_unlock(rule_info);
}
}
/* Swap invalid matches */
for (size_t entry_idx=0; entry_idx<iset->num_of_entries; ++entry_idx) {
for (int rule_idx=0; rule_idx<iset->entry_rules[entry_idx];) {
iset_match = &iset->match_db[entry_idx*iset->row_size+rule_idx];
if (!iset_match->match) {
iset_match_invalidate(iset, entry_idx, rule_idx);
}
if (iset_match->match) {
++rule_idx;
}
}
}