82e6199cfe51bfd975fe95f936254c605e22b689
[samplevnf.git] / VNFs / DPPD-PROX / bng_pkts.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
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 #ifndef _BNG_PKTS_H_
18 #define _BNG_PKTS_H_
19
20 #include <rte_ether.h>
21 #include <rte_ip.h>
22 #include <rte_udp.h>
23 #include <rte_byteorder.h>
24
25 #include "gre.h"
26 #include "mpls.h"
27 #include "qinq.h"
28 #include "arp.h"
29 #include "hash_entry_types.h"
30
31 struct cpe_pkt {
32 #ifdef USE_QINQ
33         struct qinq_hdr qinq_hdr;
34 #else
35         struct ether_hdr ether_hdr;
36 #endif
37         struct ipv4_hdr ipv4_hdr;
38         struct udp_hdr udp_hdr;
39 } __attribute__((packed));
40
41 struct cpe_packet_arp {
42         struct qinq_hdr qinq_hdr;
43         struct my_arp_t arp;
44 } __attribute__((packed));
45
46 /* Struct used for setting all the values a packet
47    going to the core netwerk. Payload may follow
48    after the headers, but no need to touch that. */
49 struct core_net_pkt_m {
50         struct ether_hdr ether_hdr;
51 #ifdef MPLS_ROUTING
52         union {
53                 struct mpls_hdr mpls;
54                 uint32_t mpls_bytes;
55         };
56 #endif
57         struct ipv4_hdr tunnel_ip_hdr;
58         struct gre_hdr gre_hdr;
59         struct ipv4_hdr ip_hdr;
60         struct udp_hdr udp_hdr;
61 } __attribute__((packed));
62
63 struct core_net_pkt {
64         struct ether_hdr ether_hdr;
65         struct ipv4_hdr tunnel_ip_hdr;
66         struct gre_hdr gre_hdr;
67         struct ipv4_hdr ip_hdr;
68         struct udp_hdr udp_hdr;
69 } __attribute__((packed));
70
71 #define UPSTREAM_DELTA   ((uint32_t)(sizeof(struct core_net_pkt) - sizeof(struct cpe_pkt)))
72 #define DOWNSTREAM_DELTA ((uint32_t)(sizeof(struct core_net_pkt_m) - sizeof(struct cpe_pkt)))
73
74 struct cpe_pkt_delta {
75         uint8_t encap[DOWNSTREAM_DELTA];
76         struct cpe_pkt pkt;
77 } __attribute__((packed));
78
79 static inline void extract_key_cpe(struct rte_mbuf *mbuf, uint64_t* key)
80 {
81         uint8_t* packet = rte_pktmbuf_mtod(mbuf, uint8_t*);
82 #ifdef USE_QINQ
83         *key = (*(uint64_t *)(packet + 12)) & 0xFF0FFFFFFF0FFFFF;
84 #else
85         *key = rte_bswap32(*(uint32_t *)(packet + 26)) & 0x00FFFFFF;
86 #endif
87 }
88
89 static inline void key_core(struct gre_hdr* gre, __attribute__((unused)) struct ipv4_hdr* ip, uint64_t* key)
90 {
91         struct cpe_key *cpe_key = (struct cpe_key*)key;
92
93         cpe_key->gre_id = rte_be_to_cpu_32(gre->gre_id) & 0xFFFFFFF;
94
95 #ifdef USE_QINQ
96         cpe_key->ip = ip->dst_addr;
97 #else
98         cpe_key->ip = 0;
99 #endif
100 }
101
102 static inline void extract_key_core(struct rte_mbuf *mbuf, uint64_t* key)
103 {
104         struct core_net_pkt *packet = rte_pktmbuf_mtod(mbuf, struct core_net_pkt *);
105         key_core(&packet->gre_hdr, &packet->ip_hdr, key);
106 }
107
108 static inline void extract_key_core_m(struct rte_mbuf *mbuf, uint64_t* key)
109 {
110         struct core_net_pkt_m *packet = rte_pktmbuf_mtod(mbuf, struct core_net_pkt_m *);
111         key_core(&packet->gre_hdr, &packet->ip_hdr, key);
112 }
113
114 #endif /* _BNG_PKTS_H_ */