aboutsummaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/common/packet_handler.c
blob: 1e97e46dba203bfbc3382d6fe49fbbd34eca24c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 *------------------------------------------------------------------
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <common.h>
#include <icmp_proto.h>

/* reply with the same data */
int
basic_packet_handler (memif_connection_t *c)
{
  int i;
  memif_buffer_t *dest, *src;

  /* in case of zero-copy the tx_buf_num will be zero, so the loop body won't
   * execute */
  for (i = 0; i < c->tx_buf_num; i++)
    {
      memcpy (c->tx_bufs[i].data, c->rx_bufs[i].data, c->rx_bufs[i].len);
    }

  return 0;
}

/* ICMPv4 and ARP handler */
int
icmp_packet_handler (memif_connection_t *c)
{
  int i;
  memif_buffer_t *dest, *src;

  /* if tx_buf_num > 0 we use non-zero-copy mode */
  if (c->tx_buf_num > 0)
    {
      for (i = 0; i < c->tx_buf_num; i++)
	{
	  uint32_t len;
	  void *packet = c->tx_bufs[i].data;

	  memcpy (c->tx_bufs[i].data, c->rx_bufs[i].data, c->rx_bufs[i].len);
	  c->tx_bufs[i].flags = c->rx_bufs[i].flags;
	  len = c->tx_bufs[i].len = c->rx_bufs[i].len;

	  while (c->rx_bufs[i].flags & MEMIF_BUFFER_FLAG_NEXT)
	    {
	      i++;
	      memcpy (c->tx_bufs[i].data, c->rx_bufs[i].data,
		      c->rx_bufs[i].len);
	      c->tx_bufs[i].flags = c->rx_bufs[i].flags;
	      len += c->tx_bufs[i].len = c->rx_bufs[i].len;
	    }

	  resolve_packet (packet, &len, c->ip_addr, c->hw_addr);
	}
    }
  else
    {
      for (i = 0; i < c->rx_buf_num; i++)
	{
	  uint32_t len = c->rx_bufs[i].len;
	  void *packet = c->rx_bufs[i].data;
	  while (c->rx_bufs[i].flags & MEMIF_BUFFER_FLAG_NEXT)
	    {
	      i++;
	      len += c->rx_bufs[i].len;
	    }
	  resolve_packet (packet, &len, c->ip_addr, c->hw_addr);
	}
    }

  return 0;
}