blob: abaa873ed87b02fe2fd1d9e2814a314de1b6ef49 [file] [log] [blame]
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301/*
2 * sfe_ipv4_udp.c
3 * Shortcut forwarding engine - IPv4 UDP implementation
4 *
5 * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05306 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05307 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21#include <linux/skbuff.h>
22#include <net/udp.h>
23#include <linux/etherdevice.h>
24#include <linux/lockdep.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053025#include <linux/version.h>
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053026
27#include "sfe_debug.h"
28#include "sfe_api.h"
29#include "sfe.h"
30#include "sfe_flow_cookie.h"
31#include "sfe_ipv4.h"
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +053032#include "sfe_pppoe.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080033#include "sfe_vlan.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053034
35/*
Amitesh Anand63be37d2021-12-24 20:51:48 +053036 * sfe_ipv4_udp_sk_deliver()
37 * Deliver the packet to the protocol handler registered with Linux.
38 * To be called under rcu_read_lock()
39 * Returns:
40 * 1 if the packet needs to be passed to Linux.
41 * 0 if the packet is processed successfully.
42 * -1 if the packet is dropped in SFE.
43 */
44static int sfe_ipv4_udp_sk_deliver(struct sk_buff *skb, struct sfe_ipv4_connection_match *cm, unsigned int ihl)
45{
46 struct udp_sock *up;
47 struct sock *sk;
48 int ret;
49 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
50
51 /*
52 * Call the decap handler for valid encap_rcv handler.
53 */
54 up = rcu_dereference(cm->up);
55 encap_rcv = READ_ONCE(up->encap_rcv);
56 if (!encap_rcv) {
57 DEBUG_ERROR("%px: sfe: Error: up->encap_rcv is NULL\n", skb);
58 return 1;
59 }
60
61#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
62 nf_reset(skb);
63#else
64 nf_reset_ct(skb);
65#endif
66
67 skb_pull(skb, ihl);
68 skb_reset_transport_header(skb);
69
70 /*
71 * Verify checksum before giving to encap_rcv handler function.
72 * TODO: The following approach is ignorant for UDPLITE for now.
73 * Instead, consider calling Linux API to do checksum validation.
74 */
75 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
76 skb->csum = inet_compute_pseudo(skb, IPPROTO_UDP);
77 if (unlikely(__skb_checksum_complete(skb))) {
78 DEBUG_ERROR("%px: sfe: Invalid udp checksum\n", skb);
79 kfree_skb(skb);
80 return -1;
81 }
82 DEBUG_TRACE("%px: sfe: udp checksum verified in s/w correctly.\n", skb);
83 }
84
85 sk = (struct sock *)up;
86
87 /*
88 * At this point, L4 checksum has already been verified and pkt is going
89 * to Linux's tunnel decap-handler. Setting ip_summed field to CHECKSUM_NONE,
90 * to ensure that later packet's inner header checksum is validated correctly.
91 * TODO: Find the fix to set skb->ip_summed = CHECKSUM_NONE;
92 */
93
94 /*
95 * encap_rcv() returns the following value:
96 * =0 if skb was successfully passed to the encap
97 * handler or was discarded by it.
98 * >0 if skb should be passed on to UDP.
99 * <0 if skb should be resubmitted as proto -N
100 */
101 ret = encap_rcv(sk, skb);
102 if (unlikely(ret)) {
103 /*
104 * If encap_rcv fails, vxlan driver drops the packet.
105 * No need to free the skb here.
106 */
107
108 DEBUG_ERROR("%px: sfe: udp-decap API return error: %d\n", skb, ret);
109 return -1;
110 }
111
112 return 0;
113}
114
115/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530116 * sfe_ipv4_recv_udp()
117 * Handle UDP packet receives and forwarding.
118 */
119int sfe_ipv4_recv_udp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
Ken Zhu88c58152021-12-09 15:12:06 -0800120 unsigned int len, struct iphdr *iph, unsigned int ihl,
121 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530122{
123 struct udphdr *udph;
124 __be32 src_ip;
125 __be32 dest_ip;
126 __be16 src_port;
127 __be16 dest_port;
128 struct sfe_ipv4_connection_match *cm;
129 u8 ttl;
Parikshit Guned31a8202022-01-05 22:15:04 +0530130 u32 service_class_id;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530131 struct net_device *xmit_dev;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530132 bool hw_csum;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530133 int err;
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530134 bool bridge_flow;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530135 int ret;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800136 bool fast_xmit;
137 netdev_features_t features;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530138
139 /*
140 * Is our packet too short to contain a valid UDP header?
141 */
142 if (unlikely(!pskb_may_pull(skb, (sizeof(struct udphdr) + ihl)))) {
143 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE);
Amitesh Anand63be37d2021-12-24 20:51:48 +0530144 DEBUG_TRACE("%px: packet too short for UDP header\n", skb);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530145 return 0;
146 }
147
148 /*
149 * Read the IP address and port information. Read the IP header data first
150 * because we've almost certainly got that in the cache. We may not yet have
151 * the UDP header cached though so allow more time for any prefetching.
152 */
153 src_ip = iph->saddr;
154 dest_ip = iph->daddr;
155
156 udph = (struct udphdr *)(skb->data + ihl);
157 src_port = udph->source;
158 dest_port = udph->dest;
159
160 rcu_read_lock();
161
162 /*
163 * Look for a connection match.
164 */
165#ifdef CONFIG_NF_FLOW_COOKIE
166 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
167 if (unlikely(!cm)) {
168 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
169 }
170#else
Amitesh Anand63be37d2021-12-24 20:51:48 +0530171 /*
172 * 5-tuple lookup for UDP flow.
173 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530174 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
175#endif
176 if (unlikely(!cm)) {
177
Amitesh Anand63be37d2021-12-24 20:51:48 +0530178 /*
179 * try a 4-tuple lookup; required for tunnels like vxlan.
180 */
181 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, 0, dest_ip, dest_port);
182 if (unlikely(!cm)) {
183 rcu_read_unlock();
184 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION);
185 DEBUG_TRACE("%px: sfe: no connection found in 4-tuple lookup.\n", skb);
186 return 0;
187 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530188 }
189
190 /*
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530191 * Source interface validate.
192 */
193 if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
194 struct sfe_ipv4_connection *c = cm->connection;
195 spin_lock_bh(&si->lock);
196 ret = sfe_ipv4_remove_connection(si, c);
197 spin_unlock_bh(&si->lock);
198
199 if (ret) {
200 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
201 }
202 rcu_read_unlock();
203 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE);
204 DEBUG_TRACE("flush on wrong source interface check failure\n");
205 return 0;
206 }
207
208 /*
209 * If our packet has beern marked as "flush on find" we can't actually
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530210 * forward it in the fast path, but now that we've found an associated
Ken Zhu88c58152021-12-09 15:12:06 -0800211 * connection we need sync its status before exception it to slow path.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530212 */
Ken Zhu88c58152021-12-09 15:12:06 -0800213 if (unlikely(sync_on_find)) {
214 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530215 rcu_read_unlock();
216 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT);
Ken Zhu88c58152021-12-09 15:12:06 -0800217 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530218 return 0;
219 }
220
221#ifdef CONFIG_XFRM
222 /*
223 * We can't accelerate the flow on this direction, just let it go
224 * through the slow path.
225 */
226 if (unlikely(!cm->flow_accel)) {
227 rcu_read_unlock();
228 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
229 return 0;
230 }
231#endif
232
Wayne Tanbb7f1782021-12-13 11:16:04 -0800233 /*
234 * Do we expect an ingress VLAN tag for this flow?
235 */
236 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
237 rcu_read_unlock();
238 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
239 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
240 return 0;
241 }
242
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530243 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
244
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530245 /*
246 * Does our TTL allow forwarding?
247 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530248 if (likely(!bridge_flow)) {
249 ttl = iph->ttl;
250 if (unlikely(ttl < 2)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800251 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530252 rcu_read_unlock();
253
Ken Zhu88c58152021-12-09 15:12:06 -0800254 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530255 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL);
256 return 0;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530257 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530258 }
259
260 /*
261 * If our packet is larger than the MTU of the transmit interface then
262 * we can't forward it easily.
263 */
264 if (unlikely(len > cm->xmit_dev_mtu)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800265 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530266 rcu_read_unlock();
267 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION);
Ken Zhu88c58152021-12-09 15:12:06 -0800268 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530269 return 0;
270 }
271
272 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530273 * Check if skb was cloned. If it was, unshare it. Because
274 * the data area is going to be written in this path and we don't want to
275 * change the cloned skb's data section.
276 */
277 if (unlikely(skb_cloned(skb))) {
278 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
279 skb = skb_unshare(skb, GFP_ATOMIC);
280 if (!skb) {
Amitesh Anand63be37d2021-12-24 20:51:48 +0530281 DEBUG_WARN("%px: Failed to unshare the cloned skb\n", skb);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530282 rcu_read_unlock();
283 return 0;
284 }
285
286 /*
287 * Update the iph and udph pointers with the unshared skb's data area.
288 */
289 iph = (struct iphdr *)skb->data;
290 udph = (struct udphdr *)(skb->data + ihl);
291 }
292
293 /*
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530294 * For PPPoE packets, match server MAC and session id
295 */
296 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530297 struct ethhdr *eth;
Nitin Shetty9af87d42022-02-11 16:25:29 +0530298 bool pppoe_match;
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530299
300 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
301 rcu_read_unlock();
302 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
303 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
304 return 0;
305 }
306
Nitin Shetty9af87d42022-02-11 16:25:29 +0530307 eth = eth_hdr(skb);
308
309 pppoe_match = (cm->pppoe_session_id == sfe_l2_pppoe_session_id_get(l2_info)) &&
310 ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source);
311
312 if (unlikely(!pppoe_match)) {
313 DEBUG_TRACE("%px: PPPoE session ID %d and %d or MAC %pM and %pM did not match\n",
314 skb, cm->pppoe_session_id, sfe_l2_pppoe_session_id_get(l2_info),
315 cm->pppoe_remote_mac, eth->h_source);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530316 rcu_read_unlock();
317 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
318 return 0;
319 }
Nitin Shetty9af87d42022-02-11 16:25:29 +0530320
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530321 skb->protocol = htons(l2_info->protocol);
322 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530323 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
324
325 /*
Nitin Shetty9af87d42022-02-11 16:25:29 +0530326 * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning
327 * the packet to linux
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530328 */
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530329 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) {
330 rcu_read_unlock();
331 DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb);
332 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
333 return 0;
334
335 }
336
337 /*
Nitin Shetty9af87d42022-02-11 16:25:29 +0530338 * For bridged flows when packet contains PPPoE header, restore the header back and forward
339 * to xmit interface
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530340 */
341 __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)));
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530342 this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530343 }
344
345 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800346 * Check if skb has enough headroom to write L2 headers
347 */
348 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
349 rcu_read_unlock();
350 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
351 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
352 return 0;
353 }
354
355 /*
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530356 * From this point on we're good to modify the packet.
357 */
358
359 /*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530360 * For PPPoE flows, add PPPoE header before L2 header is added.
361 */
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530362 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
Wayne Tanbb7f1782021-12-13 11:16:04 -0800363 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP);
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530364 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
365 }
366
367 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530368 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
369 * Note: If L4 csum at Rx was found to be incorrect, we (router) should use incremental L4 checksum here
370 * so that HW does not re-calculate/replace the L4 csum
371 */
372 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
373
374 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530375 * Do we have to perform translations of the source address/port?
376 */
377 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) {
378 u16 udp_csum;
379
380 iph->saddr = cm->xlate_src_ip;
381 udph->source = cm->xlate_src_port;
382
383 /*
384 * Do we have a non-zero UDP checksum? If we do then we need
385 * to update it.
386 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530387 if (unlikely(!hw_csum)) {
388 udp_csum = udph->check;
389 if (likely(udp_csum)) {
390 u32 sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530391
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530392 if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) {
393 sum = udp_csum + cm->xlate_src_partial_csum_adjustment;
394 } else {
395 sum = udp_csum + cm->xlate_src_csum_adjustment;
396 }
397
398 sum = (sum & 0xffff) + (sum >> 16);
399 udph->check = (u16)sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530400 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530401 }
402 }
403
404 /*
405 * Do we have to perform translations of the destination address/port?
406 */
407 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) {
408 u16 udp_csum;
409
410 iph->daddr = cm->xlate_dest_ip;
411 udph->dest = cm->xlate_dest_port;
412
413 /*
414 * Do we have a non-zero UDP checksum? If we do then we need
415 * to update it.
416 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530417 if (unlikely(!hw_csum)) {
418 udp_csum = udph->check;
419 if (likely(udp_csum)) {
420 u32 sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530421
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530422 /*
423 * TODO: Use a common API for below incremental checksum calculation
424 * for IPv4/IPv6 UDP/TCP
425 */
426 if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) {
427 sum = udp_csum + cm->xlate_dest_partial_csum_adjustment;
428 } else {
429 sum = udp_csum + cm->xlate_dest_csum_adjustment;
430 }
431
432 sum = (sum & 0xffff) + (sum >> 16);
433 udph->check = (u16)sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530434 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530435 }
436 }
437
438 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530439 * UDP sock will be valid only in decap-path.
440 * Call encap_rcv function associated with udp_sock in cm.
441 */
442 if (unlikely(cm->up)) {
443 /*
444 * Call decap handler associated with sock.
445 * Also validates UDP checksum before calling decap handler.
446 */
447 err = sfe_ipv4_udp_sk_deliver(skb, cm, ihl);
448 if (unlikely(err == -1)) {
449 rcu_read_unlock();
450 this_cpu_inc(si->stats_pcpu->packets_dropped64);
451 return 1;
452 } else if (unlikely(err == 1)) {
453 rcu_read_unlock();
454 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
455 return 0;
456 }
457
458 /*
459 * Update traffic stats.
460 */
461 atomic_inc(&cm->rx_packet_count);
462 atomic_add(len, &cm->rx_byte_count);
463
464 rcu_read_unlock();
465 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
466 DEBUG_TRACE("%px: sfe: sfe_ipv4_recv_udp -> encap_rcv done.\n", skb);
467 return 1;
468 }
469
470 /*
471 * Decrement our TTL
472 * Except when called from hook function in post-decap.
473 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530474 if (likely(!bridge_flow)) {
475 iph->ttl -= (u8)(!tun_outer);
476 }
Amitesh Anand63be37d2021-12-24 20:51:48 +0530477
478 /*
479 * Update DSCP
480 */
481 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
482 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
483 }
484
485 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530486 * If HW checksum offload is not possible, full L3 checksum and incremental L4 checksum
487 * are used to update the packet. Setting ip_summed to CHECKSUM_UNNECESSARY ensures checksum is
488 * not recalculated further in packet path.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530489 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530490 if (likely(hw_csum)) {
491 skb->ip_summed = CHECKSUM_PARTIAL;
492 } else {
493 iph->check = sfe_ipv4_gen_ip_csum(iph);
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530494 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530495
496 /*
497 * Update traffic stats.
498 */
499 atomic_inc(&cm->rx_packet_count);
500 atomic_add(len, &cm->rx_byte_count);
501
502 xmit_dev = cm->xmit_dev;
503 skb->dev = xmit_dev;
504
505 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800506 * Check to see if we need to add VLAN tags
507 */
508 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
509 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
510 }
511
512 /*
513 * Check to see if we need to write an Ethernet header.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530514 */
515 if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
516 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530517 dev_hard_header(skb, xmit_dev, ntohs(skb->protocol),
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530518 cm->xmit_dest_mac, cm->xmit_src_mac, len);
519 } else {
520 /*
521 * For the simple case we write this really fast.
522 */
523 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530524 eth->h_proto = skb->protocol;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530525 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
526 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
527 }
528 }
529
530 /*
531 * Update priority of skb.
532 */
533 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
534 skb->priority = cm->priority;
535 }
536
537 /*
538 * Mark outgoing packet.
539 */
Ken Zhu37040ea2021-09-09 21:11:15 -0700540 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
Ken Zhu306a68f2022-01-20 09:00:43 -0800541 skb->mark = cm->mark;
Parikshit Guned31a8202022-01-05 22:15:04 +0530542 /*
543 * Update service class stats if SAWF is valid.
544 */
545 if (likely(cm->sawf_valid)) {
546 service_class_id = SFE_GET_SAWF_SERVICE_CLASS(cm->mark);
547 sfe_ipv4_service_class_stats_inc(si, service_class_id, len);
548 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530549 }
550
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800551 /*
552 * For the first packets, check if it could got fast xmit.
553 */
554 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED)
555 && (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION))){
556 cm->features = netif_skb_features(skb);
557 if (likely(sfe_fast_xmit_check(skb, cm->features))) {
558 cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT;
559 }
560 cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED;
561 }
562 features = cm->features;
563
564 fast_xmit = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
565
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530566 rcu_read_unlock();
567
568 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
569
570 /*
571 * We're going to check for GSO flags when we transmit the packet so
572 * start fetching the necessary cache line now.
573 */
574 prefetch(skb_shinfo(skb));
575
576 /*
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800577 * We do per packet condition check before we could fast xmit the
578 * packet.
579 */
580 if (likely(fast_xmit && dev_fast_xmit(skb, xmit_dev, features))) {
581 this_cpu_inc(si->stats_pcpu->packets_fast_xmited64);
582 return 1;
583 }
584
585 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530586 * Mark that this packet has been fast forwarded.
587 */
588 skb->fast_forwarded = 1;
589
590 /*
591 * Send the packet on its way.
592 */
593 dev_queue_xmit(skb);
594
595 return 1;
596}