blob: 8f2b8bab03a4f781ccf53e336f3d89d6dddaaa8d [file] [log] [blame]
Tian Yangafb03452022-01-13 18:53:13 -08001/*
2 * sfe_ipv6_tunipip6.c
3 * Shortcut forwarding engine file for IPv6 TUNIPIP6
4 *
5 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <linux/skbuff.h>
21#include <linux/etherdevice.h>
22#include <linux/version.h>
23#include <net/ip6_checksum.h>
24#include <net/protocol.h>
25
26#include "sfe_debug.h"
27#include "sfe_api.h"
28#include "sfe.h"
29#include "sfe_flow_cookie.h"
30#include "sfe_ipv6.h"
31
32/*
33 * sfe_ipv6_recv_tunipip6()
34 * Handle TUNIPIP6 packet receives and forwarding.
35 */
36int sfe_ipv6_recv_tunipip6(struct sfe_ipv6 *si, struct sk_buff *skb, struct net_device *dev,
37 unsigned int len, struct ipv6hdr *iph, unsigned int ihl,
38 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
39{
40 struct sfe_ipv6_addr *src_ip;
41 struct sfe_ipv6_addr *dest_ip;
42 __be16 src_port = 0;
43 __be16 dest_port = 0;
44 struct sfe_ipv6_connection_match *cm;
45
46 DEBUG_TRACE("%px: sfe: sfe_ipv6_recv_tunipip6 called.\n", skb);
47
48 /*
49 * Read the IP address information. Read the IP header data first
50 * because we've almost certainly got that in the cache.
51 */
52 src_ip = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32;
53 dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32;
54
55 rcu_read_lock();
56
57 /*
58 * Look for a connection match.
59 */
60#ifdef CONFIG_NF_FLOW_COOKIE
61 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
62 if (unlikely(!cm)) {
63 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
64 }
65#else
66 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
67#endif
68 if (unlikely(!cm)) {
69 rcu_read_unlock();
70 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NO_CONNECTION);
71 DEBUG_TRACE("%px: no connection found\n", skb);
72 return 0;
73 }
74
75 /*
76 * If our packet has been marked as "sync on find" we will sync the status
77 * and forward it to slowpath.
78 */
79 if (unlikely(sync_on_find)) {
80 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
81 rcu_read_unlock();
82 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_SYNC_ON_FIND);
83 DEBUG_TRACE("%px: Sync on find\n", skb);
84
85 return 0;
86 }
87
88 /*
89 * If cm->proto is set, it means the decap path.
90 * Otherwise we forward the packet in encap path.
91 */
92 if(cm->proto) {
93#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
94 const struct inet6_protocol *ipprot = cm->proto;
95#else
96 struct inet6_protocol *ipprot = cm->proto;
97#endif
98 skb_pull(skb, ihl);
99 skb_reset_transport_header(skb);
100
101 /*
102 * ipprot->handler(skb) will always return 0;
103 * There is no way to tell whether the packet is dropped later in linux or not.
104 * Hence here inc the byte/packet count always.
105 */
106 atomic_inc(&cm->rx_packet_count);
107 atomic_add(len, &cm->rx_byte_count);
108 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
109 rcu_read_unlock();
110 DEBUG_TRACE("%px: %s decap done \n",skb, __func__);
111 ipprot->handler(skb);
112 return 1;
113
114 }
115
116 /*
117 * If our packet is larger than the MTU of the transmit interface then
118 * we can't forward it easily.
119 */
120 if (unlikely(len > cm->xmit_dev_mtu)) {
121 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
122 rcu_read_unlock();
123
124 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NEEDS_FRAGMENTATION);
125 DEBUG_TRACE("%px: Larger than mtu\n", skb);
126 return 0;
127 }
128
129 /*
130 * Update DSCP
131 */
132 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
133 sfe_ipv6_change_dsfield(iph, cm->dscp);
134 }
135
136 /*
137 * Update traffic stats.
138 */
139 atomic_inc(&cm->rx_packet_count);
140 atomic_add(len, &cm->rx_byte_count);
141
142 skb->dev = cm->xmit_dev;
143
144 /*
145 * Check to see if we need to write a header.
146 */
147 if (likely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
148 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
149 dev_hard_header(skb, cm->xmit_dev, ETH_P_IPV6,
150 cm->xmit_dest_mac, cm->xmit_src_mac, len);
151 } else {
152 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
153 eth->h_proto = htons(ETH_P_IPV6);
154 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
155 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
156 }
157 }
158
159 /*
160 * Update priority of skb.
161 */
162 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
163 skb->priority = cm->priority;
164 }
165
166 /*
167 * Mark outgoing packet.
168 */
169 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) {
170 skb->mark = cm->mark;
171 }
172
173 rcu_read_unlock();
174
175 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
176
177 /*
178 * We're going to check for GSO flags when we transmit the packet so
179 * start fetching the necessary cache line now.
180 */
181 prefetch(skb_shinfo(skb));
182
183 /*
184 * Mark that this packet has been fast forwarded and send it on its way.
185 */
186 skb->fast_forwarded = 1;
187 dev_queue_xmit(skb);
188
189 return 1;
190}