blob: a54f0aa5212f02b9bef6b89d728ceaf3a6514aca [file] [log] [blame]
Tian Yangd98d91b2022-03-09 14:50:12 -08001/*
2 * sfe_ipv4_tun6rd.c
3 * Shortcut forwarding engine file for IPv4 TUN6RD
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/protocol.h>
24#include <net/ip.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_ipv4.h"
31
32/*
33 * sfe_ipv4_recv_tun6rd()
34 * Handle TUN6RD packet receives and forwarding.
35 */
36int sfe_ipv4_recv_tun6rd(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
37 unsigned int len, struct iphdr *iph, unsigned int ihl,
38 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
39{
40 __be32 src_ip;
41 __be32 dest_ip;
42 __be16 src_port = 0;
43 __be16 dest_port = 0;
44 struct sfe_ipv4_connection_match *cm;
45
46 DEBUG_TRACE("%px: sfe: sfe_ipv4_recv_tun6rd 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 = iph->saddr;
53 dest_ip = iph->daddr;
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_ipv4_find_connection_match_rcu(si, dev, IPPROTO_IPV6, src_ip, src_port, dest_ip, dest_port);
64 }
65#else
66 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_IPV6, src_ip, src_port, dest_ip, dest_port);
67#endif
68 if (unlikely(!cm)) {
69 rcu_read_unlock();
70 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_NO_CONNECTION);
71 DEBUG_TRACE("%px: no tun6rd 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_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
81 rcu_read_unlock();
82 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_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 net_protocol *ipprot = cm->proto;
95#else
96 struct net_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 ipprot->handler(skb);
109 rcu_read_unlock();
110 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
111 DEBUG_TRACE("%px: %s decap done \n", skb, __func__);
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_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
122 rcu_read_unlock();
123
124 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_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_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
133 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | 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_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
148 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
149 dev_hard_header(skb, cm->xmit_dev, ETH_P_IP,
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_IP);
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_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
163 skb->priority = cm->priority;
164 }
165
166 /*
167 * Mark outgoing packet.
168 */
169 if (unlikely(cm->flags & SFE_IPV4_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}