blob: 0d96df4fc3175649d33967228afc2de2d92537b4 [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;
130 struct net_device *xmit_dev;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530131 bool hw_csum;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530132 int err;
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530133 bool bridge_flow;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530134
135 /*
136 * Is our packet too short to contain a valid UDP header?
137 */
138 if (unlikely(!pskb_may_pull(skb, (sizeof(struct udphdr) + ihl)))) {
139 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE);
Amitesh Anand63be37d2021-12-24 20:51:48 +0530140 DEBUG_TRACE("%px: packet too short for UDP header\n", skb);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530141 return 0;
142 }
143
144 /*
145 * Read the IP address and port information. Read the IP header data first
146 * because we've almost certainly got that in the cache. We may not yet have
147 * the UDP header cached though so allow more time for any prefetching.
148 */
149 src_ip = iph->saddr;
150 dest_ip = iph->daddr;
151
152 udph = (struct udphdr *)(skb->data + ihl);
153 src_port = udph->source;
154 dest_port = udph->dest;
155
156 rcu_read_lock();
157
158 /*
159 * Look for a connection match.
160 */
161#ifdef CONFIG_NF_FLOW_COOKIE
162 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
163 if (unlikely(!cm)) {
164 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
165 }
166#else
Amitesh Anand63be37d2021-12-24 20:51:48 +0530167 /*
168 * 5-tuple lookup for UDP flow.
169 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530170 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
171#endif
172 if (unlikely(!cm)) {
173
Amitesh Anand63be37d2021-12-24 20:51:48 +0530174 /*
175 * try a 4-tuple lookup; required for tunnels like vxlan.
176 */
177 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, 0, dest_ip, dest_port);
178 if (unlikely(!cm)) {
179 rcu_read_unlock();
180 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION);
181 DEBUG_TRACE("%px: sfe: no connection found in 4-tuple lookup.\n", skb);
182 return 0;
183 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530184 }
185
186 /*
Ken Zhu88c58152021-12-09 15:12:06 -0800187 * If our packet has beern marked as "sync on find" we can't actually
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530188 * forward it in the fast path, but now that we've found an associated
Ken Zhu88c58152021-12-09 15:12:06 -0800189 * connection we need sync its status before exception it to slow path.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530190 */
Ken Zhu88c58152021-12-09 15:12:06 -0800191 if (unlikely(sync_on_find)) {
192 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530193 rcu_read_unlock();
194 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT);
Ken Zhu88c58152021-12-09 15:12:06 -0800195 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530196 return 0;
197 }
198
199#ifdef CONFIG_XFRM
200 /*
201 * We can't accelerate the flow on this direction, just let it go
202 * through the slow path.
203 */
204 if (unlikely(!cm->flow_accel)) {
205 rcu_read_unlock();
206 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
207 return 0;
208 }
209#endif
210
Wayne Tanbb7f1782021-12-13 11:16:04 -0800211 /*
212 * Do we expect an ingress VLAN tag for this flow?
213 */
214 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
215 rcu_read_unlock();
216 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
217 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
218 return 0;
219 }
220
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530221 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
222
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530223 /*
224 * Does our TTL allow forwarding?
225 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530226 if (likely(!bridge_flow)) {
227 ttl = iph->ttl;
228 if (unlikely(ttl < 2)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800229 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530230 rcu_read_unlock();
231
Ken Zhu88c58152021-12-09 15:12:06 -0800232 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530233 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL);
234 return 0;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530235 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530236 }
237
238 /*
239 * If our packet is larger than the MTU of the transmit interface then
240 * we can't forward it easily.
241 */
242 if (unlikely(len > cm->xmit_dev_mtu)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800243 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530244 rcu_read_unlock();
245 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION);
Ken Zhu88c58152021-12-09 15:12:06 -0800246 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530247 return 0;
248 }
249
250 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530251 * Check if skb was cloned. If it was, unshare it. Because
252 * the data area is going to be written in this path and we don't want to
253 * change the cloned skb's data section.
254 */
255 if (unlikely(skb_cloned(skb))) {
256 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
257 skb = skb_unshare(skb, GFP_ATOMIC);
258 if (!skb) {
Amitesh Anand63be37d2021-12-24 20:51:48 +0530259 DEBUG_WARN("%px: Failed to unshare the cloned skb\n", skb);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530260 rcu_read_unlock();
261 return 0;
262 }
263
264 /*
265 * Update the iph and udph pointers with the unshared skb's data area.
266 */
267 iph = (struct iphdr *)skb->data;
268 udph = (struct udphdr *)(skb->data + ihl);
269 }
270
271 /*
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530272 * For PPPoE packets, match server MAC and session id
273 */
274 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
275 struct pppoe_hdr *ph;
276 struct ethhdr *eth;
277
278 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
279 rcu_read_unlock();
280 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
281 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
282 return 0;
283 }
284
285 ph = (struct pppoe_hdr *)(skb->head + sfe_l2_pppoe_hdr_offset_get(l2_info));
286 eth = (struct ethhdr *)(skb->head + sfe_l2_hdr_offset_get(l2_info));
287 if (unlikely(cm->pppoe_session_id != ntohs(ph->sid)) || unlikely(!(ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source)))) {
288 DEBUG_TRACE("%px: PPPoE sessions with session IDs %d and %d or server MACs %pM and %pM did not match\n",
289 skb, cm->pppoe_session_id, htons(ph->sid), cm->pppoe_remote_mac, eth->h_source);
290 rcu_read_unlock();
291 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
292 return 0;
293 }
294 skb->protocol = htons(l2_info->protocol);
295 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
296
297 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
298
299 /*
300 * If packet contains PPPOE header but CME doesn't contain PPPoE flag yet we are exceptioning the packet to linux
301 */
302 rcu_read_unlock();
303 DEBUG_TRACE("%px: CME doesn't contain PPPOE flag but packet has PPPoE header\n", skb);
304 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
305 return 0;
306 }
307
308 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800309 * Check if skb has enough headroom to write L2 headers
310 */
311 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
312 rcu_read_unlock();
313 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
314 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
315 return 0;
316 }
317
318 /*
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530319 * From this point on we're good to modify the packet.
320 */
321
322 /*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530323 * For PPPoE flows, add PPPoE header before L2 header is added.
324 */
325 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) {
Wayne Tanbb7f1782021-12-13 11:16:04 -0800326 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP);
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530327 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
328 }
329
330 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530331 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
332 * Note: If L4 csum at Rx was found to be incorrect, we (router) should use incremental L4 checksum here
333 * so that HW does not re-calculate/replace the L4 csum
334 */
335 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
336
337 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530338 * Do we have to perform translations of the source address/port?
339 */
340 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) {
341 u16 udp_csum;
342
343 iph->saddr = cm->xlate_src_ip;
344 udph->source = cm->xlate_src_port;
345
346 /*
347 * Do we have a non-zero UDP checksum? If we do then we need
348 * to update it.
349 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530350 if (unlikely(!hw_csum)) {
351 udp_csum = udph->check;
352 if (likely(udp_csum)) {
353 u32 sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530354
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530355 if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) {
356 sum = udp_csum + cm->xlate_src_partial_csum_adjustment;
357 } else {
358 sum = udp_csum + cm->xlate_src_csum_adjustment;
359 }
360
361 sum = (sum & 0xffff) + (sum >> 16);
362 udph->check = (u16)sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530363 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530364 }
365 }
366
367 /*
368 * Do we have to perform translations of the destination address/port?
369 */
370 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) {
371 u16 udp_csum;
372
373 iph->daddr = cm->xlate_dest_ip;
374 udph->dest = cm->xlate_dest_port;
375
376 /*
377 * Do we have a non-zero UDP checksum? If we do then we need
378 * to update it.
379 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530380 if (unlikely(!hw_csum)) {
381 udp_csum = udph->check;
382 if (likely(udp_csum)) {
383 u32 sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530384
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530385 /*
386 * TODO: Use a common API for below incremental checksum calculation
387 * for IPv4/IPv6 UDP/TCP
388 */
389 if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) {
390 sum = udp_csum + cm->xlate_dest_partial_csum_adjustment;
391 } else {
392 sum = udp_csum + cm->xlate_dest_csum_adjustment;
393 }
394
395 sum = (sum & 0xffff) + (sum >> 16);
396 udph->check = (u16)sum;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530397 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530398 }
399 }
400
401 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530402 * UDP sock will be valid only in decap-path.
403 * Call encap_rcv function associated with udp_sock in cm.
404 */
405 if (unlikely(cm->up)) {
406 /*
407 * Call decap handler associated with sock.
408 * Also validates UDP checksum before calling decap handler.
409 */
410 err = sfe_ipv4_udp_sk_deliver(skb, cm, ihl);
411 if (unlikely(err == -1)) {
412 rcu_read_unlock();
413 this_cpu_inc(si->stats_pcpu->packets_dropped64);
414 return 1;
415 } else if (unlikely(err == 1)) {
416 rcu_read_unlock();
417 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
418 return 0;
419 }
420
421 /*
422 * Update traffic stats.
423 */
424 atomic_inc(&cm->rx_packet_count);
425 atomic_add(len, &cm->rx_byte_count);
426
427 rcu_read_unlock();
428 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
429 DEBUG_TRACE("%px: sfe: sfe_ipv4_recv_udp -> encap_rcv done.\n", skb);
430 return 1;
431 }
432
433 /*
434 * Decrement our TTL
435 * Except when called from hook function in post-decap.
436 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530437 if (likely(!bridge_flow)) {
438 iph->ttl -= (u8)(!tun_outer);
439 }
Amitesh Anand63be37d2021-12-24 20:51:48 +0530440
441 /*
442 * Update DSCP
443 */
444 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
445 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
446 }
447
448 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530449 * If HW checksum offload is not possible, full L3 checksum and incremental L4 checksum
450 * are used to update the packet. Setting ip_summed to CHECKSUM_UNNECESSARY ensures checksum is
451 * not recalculated further in packet path.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530452 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530453 if (likely(hw_csum)) {
454 skb->ip_summed = CHECKSUM_PARTIAL;
455 } else {
456 iph->check = sfe_ipv4_gen_ip_csum(iph);
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530457 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530458
459 /*
460 * Update traffic stats.
461 */
462 atomic_inc(&cm->rx_packet_count);
463 atomic_add(len, &cm->rx_byte_count);
464
465 xmit_dev = cm->xmit_dev;
466 skb->dev = xmit_dev;
467
468 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800469 * Check to see if we need to add VLAN tags
470 */
471 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
472 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
473 }
474
475 /*
476 * Check to see if we need to write an Ethernet header.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530477 */
478 if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
479 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530480 dev_hard_header(skb, xmit_dev, ntohs(skb->protocol),
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530481 cm->xmit_dest_mac, cm->xmit_src_mac, len);
482 } else {
483 /*
484 * For the simple case we write this really fast.
485 */
486 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530487 eth->h_proto = skb->protocol;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530488 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
489 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
490 }
491 }
492
493 /*
494 * Update priority of skb.
495 */
496 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
497 skb->priority = cm->priority;
498 }
499
500 /*
501 * Mark outgoing packet.
502 */
Ken Zhu37040ea2021-09-09 21:11:15 -0700503 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
504 skb->mark = cm->connection->mark;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530505 }
506
507 rcu_read_unlock();
508
509 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
510
511 /*
512 * We're going to check for GSO flags when we transmit the packet so
513 * start fetching the necessary cache line now.
514 */
515 prefetch(skb_shinfo(skb));
516
517 /*
518 * Mark that this packet has been fast forwarded.
519 */
520 skb->fast_forwarded = 1;
521
522 /*
523 * Send the packet on its way.
524 */
525 dev_queue_xmit(skb);
526
527 return 1;
528}