mirror of
https://github.com/openvswitch/ovs
synced 2025-08-22 18:07:40 +00:00
As described in the bugzilla below, cpu_has_isa code may be compiled with some AVX512 instructions in it, because cpu.c is built as part of the libopenvswitchavx512. This is a problem when this function (supposed to probe for AVX512 instructions availability) is invoked from generic OVS code, on older CPUs that don't support them. For the same reason, dpcls_subtable_avx512_gather_probe, dp_netdev_input_outer_avx512_probe, mfex_avx512_probe and mfex_avx512_vbmi_probe are potential runtime bombs and can't either be built as part of libopenvswitchavx512. Move cpu.c to be part of the "normal" libopenvswitch. And move other helpers in generic OVS code. Note: - dpcls_subtable_avx512_gather_probe is split in two, because it also needs to do its own magic, - while moving those helpers, prefer direct calls to cpu_has_isa and avoid cast to intermediate integer variables when a simple boolean is enough, Fixes: 352b6c7116cd ("dpif-lookup: add avx512 gather implementation.") Fixes: abb807e27dd4 ("dpif-netdev: Add command to switch dpif implementation.") Fixes: 250ceddcc2d0 ("dpif-netdev/mfex: Add AVX512 based optimized miniflow extract") Fixes: b366fa2f4947 ("dpif-netdev: Call cpuid for x86 isa availability.") Reported-at: https://bugzilla.redhat.com/2100393 Reported-by: Ales Musil <amusil@redhat.com> Co-authored-by: Ales Musil <amusil@redhat.com> Signed-off-by: Ales Musil <amusil@redhat.com> Signed-off-by: David Marchand <david.marchand@redhat.com> Acked-by: Sunil Pai G <sunil.pai.g@intel.com> Acked-by: Ales Musil <amusil@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
170 lines
4.6 KiB
C
170 lines
4.6 KiB
C
/*
|
|
* Copyright (c) 2021 Intel Corporation.
|
|
*
|
|
* 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 "dpif-netdev-private-dpif.h"
|
|
#include "dpif-netdev-private-thread.h"
|
|
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
|
|
#include "cpu.h"
|
|
#include "openvswitch/dynamic-string.h"
|
|
#include "openvswitch/vlog.h"
|
|
#include "util.h"
|
|
|
|
VLOG_DEFINE_THIS_MODULE(dpif_netdev_impl);
|
|
|
|
enum dpif_netdev_impl_info_idx {
|
|
DPIF_NETDEV_IMPL_SCALAR,
|
|
DPIF_NETDEV_IMPL_AVX512
|
|
};
|
|
|
|
#if (__x86_64__ && HAVE_AVX512F && HAVE_LD_AVX512_GOOD && __SSE4_2__)
|
|
static int32_t
|
|
dp_netdev_input_outer_avx512_probe(void)
|
|
{
|
|
if (!cpu_has_isa(OVS_CPU_ISA_X86_AVX512F)
|
|
|| !cpu_has_isa(OVS_CPU_ISA_X86_BMI2)) {
|
|
return -ENOTSUP;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
/* Actual list of implementations goes here. */
|
|
static struct dpif_netdev_impl_info_t dpif_impls[] = {
|
|
/* The default scalar C code implementation. */
|
|
[DPIF_NETDEV_IMPL_SCALAR] = { .input_func = dp_netdev_input,
|
|
.probe = NULL,
|
|
.name = "dpif_scalar", },
|
|
|
|
#if (__x86_64__ && HAVE_AVX512F && HAVE_LD_AVX512_GOOD && __SSE4_2__)
|
|
/* Only available on x86_64 bit builds with SSE 4.2 used for OVS core. */
|
|
[DPIF_NETDEV_IMPL_AVX512] = { .input_func = dp_netdev_input_outer_avx512,
|
|
.probe = dp_netdev_input_outer_avx512_probe,
|
|
.name = "dpif_avx512", },
|
|
#endif
|
|
};
|
|
|
|
static dp_netdev_input_func default_dpif_func;
|
|
|
|
dp_netdev_input_func
|
|
dp_netdev_impl_get_default(void)
|
|
{
|
|
/* For the first call, this will be NULL. Compute the compile time default.
|
|
*/
|
|
if (!default_dpif_func) {
|
|
int dpif_idx = DPIF_NETDEV_IMPL_SCALAR;
|
|
|
|
/* Configure-time overriding to run test suite on all implementations. */
|
|
#if (__x86_64__ && HAVE_AVX512F && HAVE_LD_AVX512_GOOD && __SSE4_2__)
|
|
#ifdef DPIF_AVX512_DEFAULT
|
|
dp_netdev_input_func_probe probe;
|
|
|
|
/* Check if the compiled default is compatible. */
|
|
probe = dpif_impls[DPIF_NETDEV_IMPL_AVX512].probe;
|
|
if (!probe || !probe()) {
|
|
dpif_idx = DPIF_NETDEV_IMPL_AVX512;
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
VLOG_INFO("Default DPIF implementation is %s.\n",
|
|
dpif_impls[dpif_idx].name);
|
|
default_dpif_func = dpif_impls[dpif_idx].input_func;
|
|
}
|
|
|
|
return default_dpif_func;
|
|
}
|
|
|
|
void
|
|
dp_netdev_impl_get(struct ds *reply, struct dp_netdev_pmd_thread **pmd_list,
|
|
size_t n)
|
|
{
|
|
/* Add all dpif functions to reply string. */
|
|
ds_put_cstr(reply, "Available DPIF implementations:\n");
|
|
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(dpif_impls); i++) {
|
|
ds_put_format(reply, " %s (pmds: ", dpif_impls[i].name);
|
|
|
|
for (size_t j = 0; j < n; j++) {
|
|
struct dp_netdev_pmd_thread *pmd = pmd_list[j];
|
|
if (pmd->core_id == NON_PMD_CORE_ID) {
|
|
continue;
|
|
}
|
|
|
|
if (pmd->netdev_input_func == dpif_impls[i].input_func) {
|
|
ds_put_format(reply, "%u,", pmd->core_id);
|
|
}
|
|
}
|
|
|
|
ds_chomp(reply, ',');
|
|
|
|
if (ds_last(reply) == ' ') {
|
|
ds_put_cstr(reply, "none");
|
|
}
|
|
|
|
ds_put_cstr(reply, ")\n");
|
|
}
|
|
}
|
|
|
|
/* This function checks all available DPIF implementations, and selects the
|
|
* returns the function pointer to the one requested by "name".
|
|
*/
|
|
static int32_t
|
|
dp_netdev_impl_get_by_name(const char *name, dp_netdev_input_func *out_func)
|
|
{
|
|
ovs_assert(name);
|
|
ovs_assert(out_func);
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dpif_impls); i++) {
|
|
if (strcmp(dpif_impls[i].name, name) == 0) {
|
|
/* Probe function is optional - so check it is set before exec. */
|
|
if (dpif_impls[i].probe) {
|
|
int probe_err = dpif_impls[i].probe();
|
|
if (probe_err) {
|
|
*out_func = NULL;
|
|
return probe_err;
|
|
}
|
|
}
|
|
*out_func = dpif_impls[i].input_func;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return -EINVAL;
|
|
}
|
|
|
|
int32_t
|
|
dp_netdev_impl_set_default_by_name(const char *name)
|
|
{
|
|
dp_netdev_input_func new_default;
|
|
|
|
int32_t err = dp_netdev_impl_get_by_name(name, &new_default);
|
|
|
|
if (!err) {
|
|
default_dpif_func = new_default;
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|