]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-tc-offloads.c
2 * Copyright (c) 2016 Mellanox Technologies, Ltd.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "netdev-tc-offloads.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"
29 #include "netlink-socket.h"
30 #include "odp-netlink.h"
31 #include "unaligned.h"
37 VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads
);
39 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(60, 5);
41 static struct hmap ufid_tc
= HMAP_INITIALIZER(&ufid_tc
);
42 static struct ovs_mutex ufid_lock
= OVS_MUTEX_INITIALIZER
;
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
51 * @ifindex: netdev ifindex.
52 * @netdev: netdev associated with the tc rule
55 struct hmap_node ufid_node
;
56 struct hmap_node tc_node
;
61 struct netdev
*netdev
;
64 /* Remove matching ufid entry from ufid_tc hashmap. */
66 del_ufid_tc_mapping(const ovs_u128
*ufid
)
68 size_t ufid_hash
= hash_bytes(ufid
, sizeof *ufid
, 0);
69 struct ufid_tc_data
*data
;
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
)) {
79 ovs_mutex_unlock(&ufid_lock
);
83 hmap_remove(&ufid_tc
, &data
->ufid_node
);
84 hmap_remove(&ufid_tc
, &data
->tc_node
);
85 netdev_close(data
->netdev
);
87 ovs_mutex_unlock(&ufid_lock
);
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
)
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
);
100 del_ufid_tc_mapping(ufid
);
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
;
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
);
114 /* Get ufid from ufid_tc hashmap.
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.
120 * Returns handle if successful and fill prio and netdev for that ufid.
121 * Otherwise returns 0.
123 static int OVS_UNUSED
124 get_ufid_tc_mapping(const ovs_u128
*ufid
, int *prio
, struct netdev
**netdev
)
126 size_t ufid_hash
= hash_bytes(ufid
, sizeof *ufid
, 0);
127 struct ufid_tc_data
*data
;
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
)) {
137 *netdev
= netdev_ref(data
->netdev
);
139 handle
= data
->handle
;
143 ovs_mutex_unlock(&ufid_lock
);
148 /* Find ufid entry in ufid_tc hashmap using prio, handle and netdev.
149 * The result is saved in ufid.
151 * Returns true on success.
153 static bool OVS_UNUSED
154 find_ufid(int prio
, int handle
, struct netdev
*netdev
, ovs_u128
*ufid
)
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
);
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
) {
168 ovs_mutex_unlock(&ufid_lock
);
170 return (data
!= NULL
);
174 netdev_tc_flow_flush(struct netdev
*netdev
)
176 int ifindex
= netdev_get_ifindex(netdev
);
179 VLOG_ERR_RL(&error_rl
, "failed to get ifindex for %s: %s",
180 netdev_get_name(netdev
), ovs_strerror(-ifindex
));
184 return tc_flush(ifindex
);
188 netdev_tc_flow_dump_create(struct netdev
*netdev
,
189 struct netdev_flow_dump
**dump_out
)
191 struct netdev_flow_dump
*dump
= xzalloc(sizeof *dump
);
193 dump
->netdev
= netdev_ref(netdev
);
201 netdev_tc_flow_dump_destroy(struct netdev_flow_dump
*dump
)
203 netdev_close(dump
->netdev
);
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
)
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
)
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
)
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
)
253 netdev_tc_init_flow_api(struct netdev
*netdev OVS_UNUSED
)