]>
git.proxmox.com Git - ovs.git/blob - ofproto/ofproto-dpif-xlate-cache.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
17 #include "ofproto/ofproto-dpif-xlate-cache.h"
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <sys/socket.h>
30 #include "byte-order.h"
33 #include "dp-packet.h"
36 #include "mac-learning.h"
37 #include "netdev-vport.h"
38 #include "ofproto/ofproto-dpif-mirror.h"
39 #include "ofproto/ofproto-dpif-xlate.h"
40 #include "ofproto/ofproto-dpif.h"
41 #include "ofproto/ofproto-provider.h"
42 #include "openvswitch/dynamic-string.h"
43 #include "openvswitch/vlog.h"
44 #include "ovs-router.h"
46 #include "tnl-neigh-cache.h"
49 VLOG_DEFINE_THIS_MODULE(ofproto_xlate_cache
);
52 xlate_cache_init(struct xlate_cache
*xcache
)
54 ofpbuf_init(&xcache
->entries
, 120);
60 struct xlate_cache
*xcache
= xmalloc(sizeof *xcache
);
61 xlate_cache_init(xcache
);
66 xlate_cache_add_entry(struct xlate_cache
*xcache
, enum xc_type type
)
68 struct xc_entry
*entry
;
70 entry
= ofpbuf_put_zeros(&xcache
->entries
, sizeof *entry
);
77 xlate_cache_netdev(struct xc_entry
*entry
, const struct dpif_flow_stats
*stats
)
80 netdev_vport_inc_tx(entry
->dev
.tx
, stats
);
83 netdev_vport_inc_rx(entry
->dev
.rx
, stats
);
86 bfd_account_rx(entry
->dev
.bfd
, stats
);
90 /* Push stats and perform side effects of flow translation. */
92 xlate_push_stats_entry(struct xc_entry
*entry
,
93 struct dpif_flow_stats
*stats
)
97 switch (entry
->type
) {
99 ofproto_dpif_credit_table_stats(entry
->table
.ofproto
,
102 ? stats
->n_packets
: 0,
104 ? 0 : stats
->n_packets
);
107 rule_dpif_credit_stats(entry
->rule
, stats
);
110 bond_account(entry
->bond
.bond
, entry
->bond
.flow
,
111 entry
->bond
.vid
, stats
->n_bytes
);
114 xlate_cache_netdev(entry
, stats
);
117 netflow_flow_update(entry
->nf
.netflow
, entry
->nf
.flow
,
118 entry
->nf
.iface
, stats
);
121 mirror_update_stats(entry
->mirror
.mbridge
,
122 entry
->mirror
.mirrors
,
123 stats
->n_packets
, stats
->n_bytes
);
127 error
= ofproto_flow_mod_learn(entry
->learn
.ofm
, true,
128 entry
->learn
.limit
, NULL
);
130 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
131 VLOG_WARN_RL(&rl
, "xcache LEARN action execution failed.");
136 xlate_mac_learning_update(entry
->normal
.ofproto
,
137 entry
->normal
.in_port
,
138 entry
->normal
.dl_src
,
140 entry
->normal
.is_gratuitous_arp
);
143 if (stats
->tcp_flags
& (TCP_FIN
| TCP_RST
)) {
144 ofproto_rule_reduce_timeouts(&entry
->fin
.rule
->up
, entry
->fin
.idle
,
149 group_dpif_credit_stats(entry
->group
.group
, entry
->group
.bucket
,
153 /* Lookup neighbor to avoid timeout. */
154 tnl_neigh_lookup(entry
->tnl_neigh_cache
.br_name
,
155 &entry
->tnl_neigh_cache
.d_ipv6
, &dmac
);
157 case XC_TUNNEL_HEADER
:
158 if (entry
->tunnel_hdr
.operation
== ADD
) {
159 stats
->n_bytes
+= stats
->n_packets
* entry
->tunnel_hdr
.hdr_size
;
161 stats
->n_bytes
-= stats
->n_packets
* entry
->tunnel_hdr
.hdr_size
;
171 xlate_push_stats(struct xlate_cache
*xcache
,
172 struct dpif_flow_stats
*stats
)
174 if (!stats
->n_packets
) {
178 struct xc_entry
*entry
;
179 struct ofpbuf entries
= xcache
->entries
;
180 XC_ENTRY_FOR_EACH (entry
, &entries
) {
181 xlate_push_stats_entry(entry
, stats
);
186 xlate_dev_unref(struct xc_entry
*entry
)
189 netdev_close(entry
->dev
.tx
);
192 netdev_close(entry
->dev
.rx
);
194 if (entry
->dev
.bfd
) {
195 bfd_unref(entry
->dev
.bfd
);
200 xlate_cache_clear_netflow(struct netflow
*netflow
, struct flow
*flow
)
202 netflow_flow_clear(netflow
, flow
);
203 netflow_unref(netflow
);
208 xlate_cache_clear_entry(struct xc_entry
*entry
)
210 switch (entry
->type
) {
214 ofproto_rule_unref(&entry
->rule
->up
);
217 free(entry
->bond
.flow
);
218 bond_unref(entry
->bond
.bond
);
221 xlate_dev_unref(entry
);
224 xlate_cache_clear_netflow(entry
->nf
.netflow
, entry
->nf
.flow
);
227 mbridge_unref(entry
->mirror
.mbridge
);
230 ofproto_flow_mod_uninit(entry
->learn
.ofm
);
231 free(entry
->learn
.ofm
);
236 /* 'u.fin.rule' is always already held as a XC_RULE, which
237 * has already released it's reference above. */
240 ofproto_group_unref(&entry
->group
.group
->up
);
244 case XC_TUNNEL_HEADER
:
252 xlate_cache_clear(struct xlate_cache
*xcache
)
258 struct xc_entry
*entry
;
259 struct ofpbuf entries
= xcache
->entries
;
260 XC_ENTRY_FOR_EACH (entry
, &entries
) {
261 xlate_cache_clear_entry(entry
);
264 ofpbuf_clear(&xcache
->entries
);
268 xlate_cache_uninit(struct xlate_cache
*xcache
)
273 xlate_cache_clear(xcache
);
274 ofpbuf_uninit(&xcache
->entries
);
278 xlate_cache_delete(struct xlate_cache
*xcache
)
280 xlate_cache_uninit(xcache
);
284 /* Append all the entries in src into dst and remove them from src.
285 * The caller must own both xc-caches to use this function.
286 * The 'src' entries are not freed in this function as its owned by caller.
289 xlate_cache_steal_entries(struct xlate_cache
*dst
, struct xlate_cache
*src
)
294 struct ofpbuf
*src_entries
= &src
->entries
;
295 struct ofpbuf
*dst_entries
= &dst
->entries
;
298 p
= ofpbuf_put_uninit(dst_entries
, src_entries
->size
);
299 memcpy(p
, src_entries
->data
, src_entries
->size
);
300 ofpbuf_clear(src_entries
);