]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-tc-offloads.c
netdev-tc-offloads: Add ufid to tc/netdev map
[mirror_ovs.git] / lib / netdev-tc-offloads.c
1 /*
2 * Copyright (c) 2016 Mellanox Technologies, Ltd.
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 "netdev-tc-offloads.h"
19 #include <errno.h>
20 #include <linux/if_ether.h>
21 #include "openvswitch/hmap.h"
22 #include "openvswitch/match.h"
23 #include "openvswitch/ofpbuf.h"
24 #include "openvswitch/thread.h"
25 #include "openvswitch/types.h"
26 #include "openvswitch/vlog.h"
27 #include "netdev-provider.h"
28 #include "netlink.h"
29 #include "netlink-socket.h"
30 #include "odp-netlink.h"
31 #include "unaligned.h"
32 #include "util.h"
33 #include "hash.h"
34 #include "dpif.h"
35 #include "tc.h"
36
37 VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads);
38
39 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
40
41 static struct hmap ufid_tc = HMAP_INITIALIZER(&ufid_tc);
42 static struct ovs_mutex ufid_lock = OVS_MUTEX_INITIALIZER;
43
44 /**
45 * struct ufid_tc_data - data entry for ufid_tc hmap.
46 * @ufid_node: Element in @ufid_tc hash table by ufid key.
47 * @tc_node: Element in @ufid_tc hash table by prio/handle/ifindex key.
48 * @ufid: ufid assigned to the flow
49 * @prio: tc priority
50 * @handle: tc handle
51 * @ifindex: netdev ifindex.
52 * @netdev: netdev associated with the tc rule
53 */
54 struct ufid_tc_data {
55 struct hmap_node ufid_node;
56 struct hmap_node tc_node;
57 ovs_u128 ufid;
58 uint16_t prio;
59 uint32_t handle;
60 int ifindex;
61 struct netdev *netdev;
62 };
63
64 /* Remove matching ufid entry from ufid_tc hashmap. */
65 static void
66 del_ufid_tc_mapping(const ovs_u128 *ufid)
67 {
68 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
69 struct ufid_tc_data *data;
70
71 ovs_mutex_lock(&ufid_lock);
72 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
73 if (ovs_u128_equals(*ufid, data->ufid)) {
74 break;
75 }
76 }
77
78 if (!data) {
79 ovs_mutex_unlock(&ufid_lock);
80 return;
81 }
82
83 hmap_remove(&ufid_tc, &data->ufid_node);
84 hmap_remove(&ufid_tc, &data->tc_node);
85 netdev_close(data->netdev);
86 free(data);
87 ovs_mutex_unlock(&ufid_lock);
88 }
89
90 /* Add ufid entry to ufid_tc hashmap.
91 * If entry exists already it will be replaced. */
92 static void OVS_UNUSED
93 add_ufid_tc_mapping(const ovs_u128 *ufid, int prio, int handle,
94 struct netdev *netdev, int ifindex)
95 {
96 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
97 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
98 struct ufid_tc_data *new_data = xzalloc(sizeof *new_data);
99
100 del_ufid_tc_mapping(ufid);
101
102 new_data->ufid = *ufid;
103 new_data->prio = prio;
104 new_data->handle = handle;
105 new_data->netdev = netdev_ref(netdev);
106 new_data->ifindex = ifindex;
107
108 ovs_mutex_lock(&ufid_lock);
109 hmap_insert(&ufid_tc, &new_data->ufid_node, ufid_hash);
110 hmap_insert(&ufid_tc, &new_data->tc_node, tc_hash);
111 ovs_mutex_unlock(&ufid_lock);
112 }
113
114 /* Get ufid from ufid_tc hashmap.
115 *
116 * If netdev output param is not NULL then the function will return
117 * associated netdev on success and a refcount is taken on that netdev.
118 * The caller is then responsible to close the netdev.
119 *
120 * Returns handle if successful and fill prio and netdev for that ufid.
121 * Otherwise returns 0.
122 */
123 static int OVS_UNUSED
124 get_ufid_tc_mapping(const ovs_u128 *ufid, int *prio, struct netdev **netdev)
125 {
126 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
127 struct ufid_tc_data *data;
128 int handle = 0;
129
130 ovs_mutex_lock(&ufid_lock);
131 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
132 if (ovs_u128_equals(*ufid, data->ufid)) {
133 if (prio) {
134 *prio = data->prio;
135 }
136 if (netdev) {
137 *netdev = netdev_ref(data->netdev);
138 }
139 handle = data->handle;
140 break;
141 }
142 }
143 ovs_mutex_unlock(&ufid_lock);
144
145 return handle;
146 }
147
148 /* Find ufid entry in ufid_tc hashmap using prio, handle and netdev.
149 * The result is saved in ufid.
150 *
151 * Returns true on success.
152 */
153 static bool OVS_UNUSED
154 find_ufid(int prio, int handle, struct netdev *netdev, ovs_u128 *ufid)
155 {
156 int ifindex = netdev_get_ifindex(netdev);
157 struct ufid_tc_data *data;
158 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
159
160 ovs_mutex_lock(&ufid_lock);
161 HMAP_FOR_EACH_WITH_HASH(data, tc_node, tc_hash, &ufid_tc) {
162 if (data->prio == prio && data->handle == handle
163 && data->ifindex == ifindex) {
164 *ufid = data->ufid;
165 break;
166 }
167 }
168 ovs_mutex_unlock(&ufid_lock);
169
170 return (data != NULL);
171 }
172
173 int
174 netdev_tc_flow_flush(struct netdev *netdev)
175 {
176 int ifindex = netdev_get_ifindex(netdev);
177
178 if (ifindex < 0) {
179 VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s",
180 netdev_get_name(netdev), ovs_strerror(-ifindex));
181 return -ifindex;
182 }
183
184 return tc_flush(ifindex);
185 }
186
187 int
188 netdev_tc_flow_dump_create(struct netdev *netdev,
189 struct netdev_flow_dump **dump_out)
190 {
191 struct netdev_flow_dump *dump = xzalloc(sizeof *dump);
192
193 dump->netdev = netdev_ref(netdev);
194
195 *dump_out = dump;
196
197 return 0;
198 }
199
200 int
201 netdev_tc_flow_dump_destroy(struct netdev_flow_dump *dump)
202 {
203 netdev_close(dump->netdev);
204 free(dump);
205
206 return 0;
207 }
208
209 bool
210 netdev_tc_flow_dump_next(struct netdev_flow_dump *dump OVS_UNUSED,
211 struct match *match OVS_UNUSED,
212 struct nlattr **actions OVS_UNUSED,
213 struct dpif_flow_stats *stats OVS_UNUSED,
214 ovs_u128 *ufid OVS_UNUSED,
215 struct ofpbuf *rbuffer OVS_UNUSED,
216 struct ofpbuf *wbuffer OVS_UNUSED)
217 {
218 return false;
219 }
220
221 int
222 netdev_tc_flow_put(struct netdev *netdev OVS_UNUSED,
223 struct match *match OVS_UNUSED,
224 struct nlattr *actions OVS_UNUSED,
225 size_t actions_len OVS_UNUSED,
226 const ovs_u128 *ufid OVS_UNUSED,
227 struct offload_info *info OVS_UNUSED,
228 struct dpif_flow_stats *stats OVS_UNUSED)
229 {
230 return EOPNOTSUPP;
231 }
232
233 int
234 netdev_tc_flow_get(struct netdev *netdev OVS_UNUSED,
235 struct match *match OVS_UNUSED,
236 struct nlattr **actions OVS_UNUSED,
237 const ovs_u128 *ufid OVS_UNUSED,
238 struct dpif_flow_stats *stats OVS_UNUSED,
239 struct ofpbuf *buf OVS_UNUSED)
240 {
241 return EOPNOTSUPP;
242 }
243
244 int
245 netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED,
246 const ovs_u128 *ufid OVS_UNUSED,
247 struct dpif_flow_stats *stats OVS_UNUSED)
248 {
249 return EOPNOTSUPP;
250 }
251
252 int
253 netdev_tc_init_flow_api(struct netdev *netdev OVS_UNUSED)
254 {
255 return 0;
256 }