77273015c9efb4daf288a7d0c11348a11de4e06d
[vpp.git] / src / vnet / dpo / replicate_dpo.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  * @brief
17  *
18  */
19
20 #ifndef __REPLICATE_DPO_H__
21 #define __REPLICATE_DPO_H__
22
23 #include <vlib/vlib.h>
24 #include <vnet/ip/lookup.h>
25 #include <vnet/dpo/dpo.h>
26 #include <vnet/dpo/load_balance.h>
27 #include <vnet/fib/fib_types.h>
28
29 /**
30  * replicate main
31  */
32 typedef struct replicate_main_t_
33 {
34     vlib_combined_counter_main_t repm_counters;
35
36     /* per-cpu vector of cloned packets */
37     u32 **clones;
38 } replicate_main_t;
39
40 extern replicate_main_t replicate_main;
41
42 /**
43  * The number of buckets that a load-balance object can have and still
44  * fit in one cache-line
45  */
46 #define REP_NUM_INLINE_BUCKETS 4
47
48 /**
49  * The FIB DPO provieds;
50  *  - load-balancing over the next DPOs in the chain/graph
51  *  - per-route counters
52  */
53 typedef struct replicate_t_ {
54     /**
55      * number of buckets in the load-balance. always a power of 2.
56      */
57     u16 rep_n_buckets;
58
59    /**
60      * The protocol of packets that traverse this REP.
61      * need in combination with the flow hash config to determine how to hash.
62      * u8.
63      */
64     dpo_proto_t rep_proto;
65
66     /**
67      * The number of locks, which is approximately the number of users,
68      * of this load-balance.
69      * Load-balance objects of via-entries are heavily shared by recursives,
70      * so the lock count is a u32.
71      */
72     u32 rep_locks;
73
74     /**
75      * Vector of buckets containing the next DPOs, sized as repo_num
76      */
77     dpo_id_t *rep_buckets;
78
79     /**
80      * The rest of the cache line is used for buckets. In the common case
81      * where there there are less than 4 buckets, then the buckets are
82      * on the same cachlie and we save ourselves a pointer dereferance in 
83      * the data-path.
84      */
85     dpo_id_t rep_buckets_inline[REP_NUM_INLINE_BUCKETS];
86 } replicate_t;
87
88 STATIC_ASSERT(sizeof(replicate_t) <= CLIB_CACHE_LINE_BYTES,
89               "A replicate object size exceeds one cachline");
90
91 /**
92  * Flags controlling load-balance formatting/display
93  */
94 typedef enum replicate_format_flags_t_ {
95     REPLICATE_FORMAT_NONE,
96     REPLICATE_FORMAT_DETAIL = (1 << 0),
97 } replicate_format_flags_t;
98
99 extern index_t replicate_create(u32 num_buckets,
100                                 dpo_proto_t rep_proto);
101 extern void replicate_multipath_update(
102     const dpo_id_t *dpo,
103     load_balance_path_t *next_hops);
104
105 extern void replicate_set_bucket(index_t repi,
106                                     u32 bucket,
107                                     const dpo_id_t *next);
108
109 extern u8* format_replicate(u8 * s, va_list * args);
110
111 extern const dpo_id_t *replicate_get_bucket(index_t repi,
112                                                u32 bucket);
113 extern int replicate_is_drop(const dpo_id_t *dpo);
114
115 /**
116  * The encapsulation breakages are for fast DP access
117  */
118 extern replicate_t *replicate_pool;
119 static inline replicate_t*
120 replicate_get (index_t repi)
121 {
122     return (pool_elt_at_index(replicate_pool, repi));
123 }
124
125 #define REP_HAS_INLINE_BUCKETS(_rep)            \
126     ((_rep)->rep_n_buckets <= REP_NUM_INLINE_BUCKETS)
127
128 static inline const dpo_id_t *
129 replicate_get_bucket_i (const replicate_t *rep,
130                            u32 bucket)
131 {
132     ASSERT(bucket < rep->rep_n_buckets);
133
134     if (PREDICT_TRUE(REP_HAS_INLINE_BUCKETS(rep)))
135     {
136         return (&rep->rep_buckets_inline[bucket]);
137     }
138     else
139     {
140         return (&rep->rep_buckets[bucket]);
141     }
142 }
143
144 extern void replicate_module_init(void);
145
146 #endif