avf: support generic flow
[vpp.git] / src / plugins / avf / avf_rss_lib.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2022 Intel and/or its affiliates.
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
18 #include <vppinfra/mem.h>
19 #include "avf_advanced_flow.h"
20
21 int
22 avf_rss_cfg_create (struct virtchnl_rss_cfg **rss_cfg, int tunnel_level)
23 {
24   *rss_cfg = clib_mem_alloc (sizeof (**rss_cfg));
25   if ((*rss_cfg) == NULL)
26     return -1;
27
28   clib_memset (*rss_cfg, 0, sizeof (**rss_cfg));
29
30   (*rss_cfg)->proto_hdrs.tunnel_level = tunnel_level;
31
32   return 0;
33 }
34
35 int
36 avf_rss_rcfg_destroy (struct virtchnl_rss_cfg *rss_cfg)
37 {
38   clib_mem_free (rss_cfg);
39
40   return 0;
41 }
42
43 int
44 avf_rss_parse_action (const struct avf_flow_action actions[],
45                       struct virtchnl_rss_cfg *rss_cfg,
46                       struct avf_flow_error *error)
47 {
48   const struct avf_flow_action_rss *rss;
49   int ret;
50
51   rss = actions->conf;
52
53   if (rss->func == AVF_ETH_HASH_FUNCTION_SIMPLE_XOR)
54     {
55       rss_cfg->rss_algorithm = VIRTCHNL_RSS_ALG_XOR_ASYMMETRIC;
56       ret = avf_flow_error_set (error, AVF_FAILURE, AVF_FLOW_ERROR_TYPE_ACTION,
57                                 actions, "simple xor is not supported.");
58       return ret;
59     }
60   else if (rss->func == AVF_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ)
61     {
62       rss_cfg->rss_algorithm = VIRTCHNL_RSS_ALG_TOEPLITZ_SYMMETRIC;
63     }
64   else
65     {
66       rss_cfg->rss_algorithm = VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC;
67     }
68
69   return 0;
70 }
71
72 int
73 avf_rss_parse_generic_pattern (struct virtchnl_rss_cfg *rss_cfg,
74                                struct avf_flow_item avf_items[],
75                                struct avf_flow_error *error)
76 {
77   struct avf_flow_item *item = avf_items;
78   u8 *pkt_buf, *msk_buf;
79   u16 spec_len, pkt_len;
80
81   spec_len = clib_strnlen (item->spec, VIRTCHNL_MAX_SIZE_GEN_PACKET);
82   pkt_len = spec_len / 2;
83
84   pkt_buf = clib_mem_alloc (pkt_len);
85   msk_buf = clib_mem_alloc (pkt_len);
86
87   avf_parse_generic_pattern (item, pkt_buf, msk_buf, spec_len);
88
89   clib_memcpy (rss_cfg->proto_hdrs.raw.spec, pkt_buf, pkt_len);
90   clib_memcpy (rss_cfg->proto_hdrs.raw.mask, msk_buf, pkt_len);
91
92   rss_cfg->proto_hdrs.count = 0;
93   rss_cfg->proto_hdrs.tunnel_level = 0;
94   rss_cfg->proto_hdrs.raw.pkt_len = pkt_len;
95
96   clib_mem_free (pkt_buf);
97   clib_mem_free (msk_buf);
98
99   return 0;
100 }
101
102 /* Used for common flow creation */
103 int
104 avf_rss_parse_pattern (struct virtchnl_rss_cfg *rss_cfg,
105                        struct avf_flow_item avf_items[],
106                        struct avf_flow_error *error)
107 {
108   return -1;
109 }
110
111 int
112 avf_rss_rule_create (struct avf_flow_vc_ctx *ctx,
113                      struct virtchnl_rss_cfg *rss_cfg)
114 {
115   int ret;
116
117   ret = ctx->vc_op (ctx->vc_hdl, VIRTCHNL_ADV_OP_ADD_RSS_CFG, rss_cfg,
118                     sizeof (*rss_cfg), 0, 0);
119
120   return ret;
121 }
122
123 int
124 avf_rss_rule_destroy (struct avf_flow_vc_ctx *ctx,
125                       struct virtchnl_rss_cfg *rss_cfg)
126 {
127   int ret;
128
129   ret = ctx->vc_op (ctx->vc_hdl, VIRTCHNL_ADV_OP_DEL_RSS_CFG, rss_cfg,
130                     sizeof (*rss_cfg), 0, 0);
131
132   return ret;
133 }
134
135 /*
136  * fd.io coding-style-patch-verification: ON
137  *
138  * Local Variables:
139  * eval: (c-set-style "gnu")
140  * End:
141  */