fib: midchain adjacency optimisations
[vpp.git] / src / vnet / adj / adj_dp.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef __ADJ_DP_H__
17 #define __ADJ_DP_H__
18
19 #include <vnet/adj/adj.h>
20 #include <vnet/tunnel/tunnel_dp.h>
21
22 static_always_inline void
23 adj_midchain_ipip44_fixup (vlib_main_t * vm,
24                            const ip_adjacency_t * adj,
25                            vlib_buffer_t * b)
26 {
27   tunnel_encap_decap_flags_t flags;
28   ip4_header_t *ip4;
29
30   flags = pointer_to_uword (adj->sub_type.midchain.fixup_data);
31
32   ip4 = vlib_buffer_get_current (b);
33   ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
34
35   if (PREDICT_TRUE(TUNNEL_ENCAP_DECAP_FLAG_NONE == flags))
36   {
37       ip_csum_t sum;
38       u16 old,new;
39
40       old = 0;
41       new = ip4->length;
42
43       sum = ip4->checksum;
44       sum = ip_csum_update (sum, old, new, ip4_header_t, length);
45       ip4->checksum = ip_csum_fold (sum);
46   }
47   else
48   {
49       tunnel_encap_fixup_4o4 (flags, ip4 + 1, ip4);
50       ip4->checksum = ip4_header_checksum (ip4);
51   }
52 }
53
54 static_always_inline void
55 adj_midchain_fixup (vlib_main_t *vm,
56                     const ip_adjacency_t *adj,
57                     vlib_buffer_t * b)
58 {
59     if (PREDICT_TRUE(adj->rewrite_header.flags & VNET_REWRITE_FIXUP_IP4_O_4))
60         adj_midchain_ipip44_fixup (vm, adj, b);
61     else if (adj->sub_type.midchain.fixup_func)
62         adj->sub_type.midchain.fixup_func
63             (vm, adj, b, adj->sub_type.midchain.fixup_data);
64 }
65
66 #endif