Coverage Report

Created: 2023-03-26 07:41

/src/openvswitch/lib/dpif-netdev-lookup.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020 Intel Corporation.
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 <errno.h>
19
#include "dpif-netdev-lookup.h"
20
21
#include "cpu.h"
22
#include "openvswitch/vlog.h"
23
24
VLOG_DEFINE_THIS_MODULE(dpif_netdev_lookup);
25
#define DPCLS_IMPL_AVX512_CHECK (__x86_64__ && HAVE_AVX512F \
26
    && HAVE_LD_AVX512_GOOD && HAVE_AVX512BW && __SSE4_2__)
27
28
#if DPCLS_IMPL_AVX512_CHECK
29
static dpcls_subtable_lookup_func
30
dpcls_subtable_avx512_gather_probe(uint32_t u0_bits, uint32_t u1_bits)
31
{
32
    if (!cpu_has_isa(OVS_CPU_ISA_X86_AVX512F)
33
        || !cpu_has_isa(OVS_CPU_ISA_X86_BMI2)) {
34
        return NULL;
35
    }
36
37
    return dpcls_subtable_avx512_gather_probe__(u0_bits, u1_bits,
38
        cpu_has_isa(OVS_CPU_ISA_X86_VPOPCNTDQ));
39
}
40
#endif
41
42
/* Actual list of implementations goes here */
43
static struct dpcls_subtable_lookup_info_t subtable_lookups[] = {
44
    /* The autovalidator implementation will not be used by default, it must
45
     * be enabled at compile time to be the default lookup implementation. The
46
     * user may enable it at runtime using the normal "prio-set" command if
47
     * desired. The compile time default switch is here to enable all unit
48
     * tests to transparently run with the autovalidator.
49
     */
50
#ifdef DPCLS_AUTOVALIDATOR_DEFAULT
51
    { .prio = 255,
52
#else
53
    { .prio = 0,
54
#endif
55
      .probe = dpcls_subtable_autovalidator_probe,
56
      .name = "autovalidator",
57
      .usage_cnt = ATOMIC_COUNT_INIT(0), },
58
59
    /* The default scalar C code implementation. */
60
    { .prio = 1,
61
      .probe = dpcls_subtable_generic_probe,
62
      .name = "generic",
63
      .usage_cnt = ATOMIC_COUNT_INIT(0), },
64
65
#if DPCLS_IMPL_AVX512_CHECK
66
    /* Only available on x86_64 bit builds with SSE 4.2 used for OVS core. */
67
    { .prio = 0,
68
      .probe = dpcls_subtable_avx512_gather_probe,
69
      .name = "avx512_gather",
70
      .usage_cnt = ATOMIC_COUNT_INIT(0), },
71
#else
72
    /* Disabling AVX512 at compile time, as compile time requirements not met.
73
     * This could be due to a number of reasons:
74
     *  1) core OVS is not compiled with SSE4.2 instruction set.
75
     *     The SSE42 instructions are required to use CRC32 ISA for high-
76
     *     performance hashing. Consider ./configure of OVS with -msse42 (or
77
     *     newer) to enable CRC32 hashing and higher performance.
78
     *  2) The assembler in binutils versions 2.30 and 2.31 has bugs in AVX512
79
     *     assembly. Compile time probes check for this assembler issue, and
80
     *     disable the HAVE_LD_AVX512_GOOD check if an issue is detected.
81
     *     Please upgrade binutils, or backport this binutils fix commit:
82
     *     2069ccaf8dc28ea699bd901fdd35d90613e4402a
83
     */
84
#endif
85
};
86
87
int
88
dpcls_subtable_lookup_info_get(struct dpcls_subtable_lookup_info_t **out_ptr)
89
0
{
90
0
    if (out_ptr == NULL) {
91
0
        return -1;
92
0
    }
93
94
0
    *out_ptr = subtable_lookups;
95
0
    return ARRAY_SIZE(subtable_lookups);
96
0
}
97
98
/* sets the priority of the lookup function with "name". */
99
int
100
dpcls_subtable_set_prio(const char *name, uint8_t priority)
101
0
{
102
0
    for (int i = 0; i < ARRAY_SIZE(subtable_lookups); i++) {
103
0
        if (strcmp(name, subtable_lookups[i].name) == 0) {
104
0
                subtable_lookups[i].prio = priority;
105
0
                VLOG_INFO("Subtable function '%s' set priority to %d\n",
106
0
                         name, priority);
107
0
                return 0;
108
0
        }
109
0
    }
110
0
    VLOG_WARN("Subtable function '%s' not found, failed to set priority\n",
111
0
              name);
112
0
    return -EINVAL;
113
0
}
114
115
dpcls_subtable_lookup_func
116
dpcls_subtable_get_best_impl(uint32_t u0_bit_count, uint32_t u1_bit_count,
117
                             struct dpcls_subtable_lookup_info_t **info)
118
0
{
119
0
    struct dpcls_subtable_lookup_info_t *best_info = NULL;
120
0
    dpcls_subtable_lookup_func best_func = NULL;
121
0
    int prio = -1;
122
123
    /* Iter over each subtable impl, and get highest priority one. */
124
0
    for (int i = 0; i < ARRAY_SIZE(subtable_lookups); i++) {
125
0
        struct dpcls_subtable_lookup_info_t *impl_info = &subtable_lookups[i];
126
0
        dpcls_subtable_lookup_func probed_func;
127
128
0
        if (impl_info->prio <= prio) {
129
0
            continue;
130
0
        }
131
132
0
        probed_func = subtable_lookups[i].probe(u0_bit_count,
133
0
                                                u1_bit_count);
134
0
        if (!probed_func) {
135
0
            continue;
136
0
        }
137
138
0
        best_func = probed_func;
139
0
        best_info = impl_info;
140
0
        prio = impl_info->prio;
141
0
    }
142
143
    /* Programming error - we must always return a valid func ptr. */
144
0
    ovs_assert(best_func != NULL && best_info != NULL);
145
146
0
    VLOG_DBG("Subtable lookup function '%s' with units (%d,%d), priority %d\n",
147
0
             best_info->name, u0_bit_count, u1_bit_count, prio);
148
149
0
    if (info) {
150
0
        *info = best_info;
151
0
    }
152
0
    return best_func;
153
0
}
154
155
void
156
dpcls_info_inc_usage(struct dpcls_subtable_lookup_info_t *info)
157
0
{
158
0
    if (info) {
159
0
        atomic_count_inc(&info->usage_cnt);
160
0
    }
161
0
}
162
163
void
164
dpcls_info_dec_usage(struct dpcls_subtable_lookup_info_t *info)
165
0
{
166
0
    if (info) {
167
0
        atomic_count_dec(&info->usage_cnt);
168
0
    }
169
0
}
170
171
void
172
dpcls_impl_print_stats(struct ds *reply)
173
0
{
174
0
    struct dpcls_subtable_lookup_info_t *lookup_funcs = NULL;
175
0
    int count = dpcls_subtable_lookup_info_get(&lookup_funcs);
176
177
    /* Add all DPCLS functions to reply string. */
178
0
    ds_put_cstr(reply, "Available dpcls implementations:\n");
179
180
0
    for (int i = 0; i < count; i++) {
181
0
        ds_put_format(reply, "  %s (Use count: %d, Priority: %d",
182
0
                      lookup_funcs[i].name,
183
0
                      atomic_count_get(&lookup_funcs[i].usage_cnt),
184
0
                      lookup_funcs[i].prio);
185
186
0
        if (ds_last(reply) == ' ') {
187
0
            ds_put_cstr(reply, "none");
188
0
        }
189
190
0
        ds_put_cstr(reply, ")\n");
191
0
    }
192
193
0
}