aboutsummaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/icmp_responder
diff options
context:
space:
mode:
Diffstat (limited to 'extras/libmemif/examples/icmp_responder')
-rw-r--r--extras/libmemif/examples/icmp_responder/icmp_proto.c524
-rw-r--r--extras/libmemif/examples/icmp_responder/icmp_proto.h45
-rw-r--r--extras/libmemif/examples/icmp_responder/main.c417
3 files changed, 0 insertions, 986 deletions
diff --git a/extras/libmemif/examples/icmp_responder/icmp_proto.c b/extras/libmemif/examples/icmp_responder/icmp_proto.c
deleted file mode 100644
index 785aebd5567..00000000000
--- a/extras/libmemif/examples/icmp_responder/icmp_proto.c
+++ /dev/null
@@ -1,524 +0,0 @@
-/*
- *------------------------------------------------------------------
- * Copyright (c) 2017 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 <stdint.h>
-#include <net/if.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/uio.h>
-#include <sys/mman.h>
-#include <sys/prctl.h>
-#include <inttypes.h>
-#include <string.h>
-#include <stdio.h>
-#include <netdb.h>
-#include <linux/ip.h>
-#include <linux/icmp.h>
-#include <arpa/inet.h>
-#include <stdlib.h>
-#include <netinet/if_ether.h>
-#include <net/if_arp.h>
-#include <asm/byteorder.h>
-#include <byteswap.h>
-#include <assert.h>
-
-#include <icmp_proto.h>
-
-static uint16_t
-cksum (void *addr, ssize_t len)
-{
- char *data = (char *) addr;
-
- uint32_t acc = 0xffff;
-
- ssize_t i;
- for (i = 0; (i + 1) < len; i += 2)
- {
- uint16_t word;
- memcpy (&word, data + i, 2);
- acc += ntohs (word);
- if (acc > 0xffff)
- acc -= 0xffff;
- }
-
- if (len & 1)
- {
- uint16_t word = 0;
- memcpy (&word, data + len - 1, 1);
- acc += ntohs (word);
- if (acc > 0xffff)
- acc -= 0xffff;
- }
- return htons (~acc);
-}
-
-int
-print_packet (void *pck)
-{
- if (pck == NULL)
- {
- printf ("ICMP_PROTO: no data\n");
- return -1;
- }
- struct iphdr *ip;
- struct icmphdr *icmp;
- ip = (struct iphdr *) pck;
- icmp = (struct icmphdr *) (pck + sizeof (struct iphdr));
- printf ("received packet:\n");
- printf ("\tiphdr:\n");
- printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n",
- ip->ihl, ip->version, __bswap_16 (ip->tot_len), ip->id);
- printf ("\t\tprotocol: %u\n", ip->protocol);
-
- printf ("\t\tsaddr: ");
- int i;
- for (i = 0; i < 4; i++)
- {
- printf ("%u.", ((uint8_t *) & ip->saddr)[i]);
- }
- printf ("\n");
-
- printf ("\t\tdaddr: ");
- for (i = 0; i < 4; i++)
- {
- printf ("%u.", ((uint8_t *) & ip->daddr)[i]);
- }
- printf ("\n");
- printf ("\ticmphdr:\n");
- printf ("\t\ttype: %s\n",
- (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY");
-
- return 0;
-}
-
-static ssize_t
-resolve_arp (void *arp)
-{
- struct arphdr *resp = (struct arphdr *) arp;
-
- resp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
-
- resp->ar_pro = __bswap_16 (0x0800);
-
- resp->ar_hln = 6;
- resp->ar_pln = 4;
-
- resp->ar_op = __bswap_16 (ARPOP_REPLY);
-
- return sizeof (struct arphdr);
-}
-
-static ssize_t
-resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp,
- uint8_t ip_addr[4])
-{
- struct ether_arp *resp = (struct ether_arp *) eth_arp_resp;
-
- resolve_arp (&resp->ea_hdr);
-
- memcpy (resp->arp_tha, eth_arp->arp_sha, 6);
- memcpy (resp->arp_tpa, eth_arp->arp_spa, 4);
-
- memcpy (resp->arp_sha,
- (((struct ether_header *) (eth_arp_resp -
- sizeof (struct
- ether_header)))->ether_shost),
- 6);
-
- memcpy (resp->arp_spa, ip_addr, 4);
-
- return sizeof (struct ether_arp);
-}
-
-static ssize_t
-resolve_eth (struct ether_header *eth, void *eth_resp)
-{
- struct ether_header *resp = (struct ether_header *) eth_resp;
- memcpy (resp->ether_dhost, eth->ether_shost, 6);
-
- uint8_t hw_addr[6];
- int i;
- for (i = 0; i < 6; i++)
- {
- hw_addr[i] = 'a';
- }
- memcpy (resp->ether_shost, hw_addr, 6);
-
- resp->ether_type = eth->ether_type;
-
- return sizeof (struct ether_header);
-}
-
-static ssize_t
-resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
-{
- struct iphdr *resp = (struct iphdr *) ip_resp;
- resp->ihl = 5;
- resp->version = 4;
- resp->tos = 0;
- /*len updated later */
- resp->tot_len = 0x0000;
- resp->id = 0;
- resp->frag_off = 0;
- resp->ttl = 0x40;
- resp->protocol = 1;
- ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
- ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
- ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
- ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
- resp->daddr = ip->saddr;
-
- /* resp->check = cksum (resp, sizeof (struct iphdr)); */
-
- return sizeof (struct iphdr);
-}
-
-static ssize_t
-resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
-{
- struct icmphdr *resp = (struct icmphdr *) icmp_resp;
- resp->type = 0x00;
- resp->code = 0;
- resp->un.echo.id = icmp->un.echo.id;
- resp->un.echo.sequence = icmp->un.echo.sequence;
-
- /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */
-
- return sizeof (struct icmphdr);
-}
-
-int
-resolve_packet (void *in_pck, ssize_t in_size,
- void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct ether_arp *eah;
- struct iphdr *ip, *ip_out;
- struct icmphdr *icmp;
- *out_size = 0;
-
- if ((in_pck == NULL) || (out_pck == NULL))
- return -1;
-
- eh = (struct ether_header *) in_pck;
- *out_size = resolve_eth (eh, out_pck);
-
- if (eh->ether_type == 0x0608)
- {
- eah = (struct ether_arp *) (in_pck + *out_size);
- *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
-
- }
- else if (eh->ether_type == 0x0008)
- {
-#ifdef ICMP_DBG
- print_packet (in_pck + *out_size);
-#endif
- ip = (struct iphdr *) (in_pck + *out_size);
- ip_out = (struct iphdr *) (out_pck + *out_size);
- *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
- if (ip->protocol == 1)
- {
- icmp = (struct icmphdr *) (in_pck + *out_size);
- *out_size += resolve_icmp (icmp, out_pck + *out_size);
- ((struct icmphdr *) (out_pck + *out_size -
- sizeof (struct icmphdr)))->checksum =
- cksum (out_pck + *out_size - sizeof (struct icmphdr),
- sizeof (struct icmphdr));
- /* payload */
- memcpy (out_pck + *out_size, in_pck + *out_size,
- in_size - *out_size);
- *out_size = in_size;
- ip_out->tot_len =
- __bswap_16 (*out_size - sizeof (struct ether_header));
- ip_out->check = cksum (ip_out, sizeof (struct iphdr));
- }
- }
- return 0;
-}
-
-static ssize_t
-generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
-{
- uint8_t hw_addr[6];
- int i;
- for (i = 0; i < 6; i++)
- {
- hw_addr[i] = 'a';
- }
- memcpy (eh->ether_shost, hw_addr, 6);
- memcpy (eh->ether_dhost, hw_daddr, 6);
-
- eh->ether_type = 0x0008;
-
- return sizeof (struct ether_header);
-}
-
-static ssize_t
-generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
-{
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- /*len updated later */
- ip->tot_len = 0x5400;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- /* saddr */
- ((uint8_t *) & ip->saddr)[0] = saddr[0];
- ((uint8_t *) & ip->saddr)[1] = saddr[1];
- ((uint8_t *) & ip->saddr)[2] = saddr[2];
- ((uint8_t *) & ip->saddr)[3] = saddr[3];
- /* daddr */
- ((uint8_t *) & ip->daddr)[0] = daddr[0];
- ((uint8_t *) & ip->daddr)[1] = daddr[1];
- ((uint8_t *) & ip->daddr)[2] = daddr[2];
- ((uint8_t *) & ip->daddr)[3] = daddr[3];
-
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return sizeof (struct iphdr);
-}
-
-static ssize_t
-generate_icmp (struct icmphdr *icmp, uint32_t seq)
-{
- icmp->type = ICMP_ECHO;
- icmp->code = 0;
- icmp->un.echo.id = 0;
- icmp->un.echo.sequence = seq;
-
- return sizeof (struct icmphdr);
-}
-
-int
-generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
-
- *size = 0;
-
- eh = (struct ether_header *) pck;
- *size += generate_eth (eh, hw_daddr);
-
- ip = (struct iphdr *) (pck + *size);
- *size += generate_ip (ip, saddr, daddr);
-
- icmp = (struct icmphdr *) (pck + *size);
- *size += generate_icmp (icmp, seq);
-
- ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
- cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
-
- ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
- ip->check = 0;
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return 0;
-}
-
-int
-generate_packet2 (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
- icmpr_flow_mode_t mode)
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
-
- *size = 0;
-
- if (mode == ICMPR_FLOW_MODE_ETH)
- {
- eh = (struct ether_header *) pck;
- *size += generate_eth (eh, hw_daddr);
- }
-
- ip = (struct iphdr *) (pck + *size);
- *size += generate_ip (ip, saddr, daddr);
-
- icmp = (struct icmphdr *) (pck + *size);
- *size += generate_icmp (icmp, seq);
-
- ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
- cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
-
- ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
- ip->check = 0;
- ip->check = cksum (ip, sizeof (struct iphdr));
-
- return 0;
-}
-
-#define GET_HEADER(out,hdr,src,off) do { \
- out = (hdr*)(src + off); \
- off += sizeof (hdr); \
- } while (0)
-
-int
-resolve_packet2 (void *pck, uint32_t * size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct ether_arp *eah;
- struct iphdr *ip;
- struct icmphdr *icmp;
- uint32_t offset = 0;
-
- if (pck == NULL)
- return 0;
-
- GET_HEADER (eh, struct ether_header, pck, offset);
-
- memcpy (eh->ether_dhost, eh->ether_shost, 6);
- memcpy (eh->ether_shost, "aaaaaa", 6);
-
- if (eh->ether_type == 0x0608)
- {
- GET_HEADER (eah, struct ether_arp, pck, offset);
- struct arphdr *arp = &eah->ea_hdr;
-
- arp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
- arp->ar_pro = __bswap_16 (0x0800);
-
- arp->ar_hln = 6;
- arp->ar_pln = 4;
-
- arp->ar_op = __bswap_16 (ARPOP_REPLY);
-
- memcpy (eah->arp_tha, eah->arp_sha, 6);
- memcpy (eah->arp_tpa, eah->arp_spa, 4);
-
- memcpy (eah->arp_sha, eh->ether_shost, 6);
- memcpy (eah->arp_spa, ip_addr, 4);
- }
-
- else if (eh->ether_type == 0x0008)
- {
- GET_HEADER (ip, struct iphdr, pck, offset);
-
- if (ip->protocol == 1)
- {
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- ip->tot_len = 0x0000;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- ip->check = 0x0000;
- ip->daddr = ip->saddr;
- ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
- ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
- ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
- ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
-
- GET_HEADER (icmp, struct icmphdr, pck, offset);
-
- icmp->type = 0x00;
- icmp->code = 0;
- icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
-
- /* rest is payload */
- offset = *size;
-
- ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
- ip->check = cksum (ip, sizeof (struct iphdr));
- }
- }
-
- assert (offset == *size && "unsupported protocol");
- return 0;
-}
-
-
-int
-resolve_packet3 (void **pck_, uint32_t * size, uint8_t ip_addr[4])
-{
- struct ether_header *eh;
- struct iphdr *ip;
- struct icmphdr *icmp;
- int32_t offset = 0;
- uint16_t encap_size = sizeof (struct ether_header);
- void *pck = *pck_;
-
- if (pck == NULL)
- return 0;
-
- *pck_ -= encap_size;
- offset -= encap_size;
-
- GET_HEADER (eh, struct ether_header, pck, offset);
-
- uint8_t hw_daddr[6];
- memset (hw_daddr, 0, sizeof (uint8_t) * 6);
-
- generate_eth (eh, hw_daddr);
-
- if (eh->ether_type == 0x0008)
- {
- GET_HEADER (ip, struct iphdr, pck, offset);
-
- if (ip->protocol == 1)
- {
- ip->ihl = 5;
- ip->version = 4;
- ip->tos = 0;
- ip->tot_len = 0x0000;
- ip->id = 0;
- ip->frag_off = 0;
- ip->ttl = 0x40;
- ip->protocol = 1;
- ip->check = 0x0000;
- ip->daddr = ip->saddr;
- ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
- ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
- ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
- ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
-
- GET_HEADER (icmp, struct icmphdr, pck, offset);
-
- icmp->type = 0x00;
- icmp->code = 0;
- icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
-
- /* rest is payload */
- offset = *size;
-
- ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
- ip->check = cksum (ip, sizeof (struct iphdr));
- }
- }
-
- offset += encap_size;
-
- assert (offset != *size &&
- "new packet length must be increased by encap size");
-
- /* overwrite packet size */
- *size = offset;
-
- return 0;
-}
diff --git a/extras/libmemif/examples/icmp_responder/icmp_proto.h b/extras/libmemif/examples/icmp_responder/icmp_proto.h
deleted file mode 100644
index 1575719bd6a..00000000000
--- a/extras/libmemif/examples/icmp_responder/icmp_proto.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *------------------------------------------------------------------
- * Copyright (c) 2017 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.
- *------------------------------------------------------------------
- */
-
-#ifndef _ICMP_PROTO_H_
-#define _ICMP_PROTO_H_
-
-typedef enum
-{
- ICMPR_FLOW_MODE_ETH = 0,
- ICMPR_FLOW_MODE_IP,
-} icmpr_flow_mode_t;
-
-int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck,
- uint32_t * out_size, uint8_t ip_addr[4]);
-
-/* resolve packet in place */
-int resolve_packet2 (void *pck, uint32_t * size, uint8_t ip_addr[4]);
-
-/* resolve packet in place and add eth encap */
-int resolve_packet3 (void **pck, uint32_t * size, uint8_t ip_addr[4]);
-
-int generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq);
-
-int generate_packet2 (void *pck, uint32_t * size, uint8_t saddr[4],
- uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
- icmpr_flow_mode_t);
-
-int print_packet (void *pck);
-
-#endif /* _ICMP_PROTO_H_ */
diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c
deleted file mode 100644
index 517512340e0..00000000000
--- a/extras/libmemif/examples/icmp_responder/main.c
+++ /dev/null
@@ -1,417 +0,0 @@
-/*
- *------------------------------------------------------------------
- * Copyright (c) 2017 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 <stdlib.h>
-#include <stdint.h>
-#include <net/if.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/uio.h>
-#include <sys/mman.h>
-#include <sys/prctl.h>
-#include <inttypes.h>
-#include <string.h>
-#include <stdio.h>
-#include <netdb.h>
-#include <linux/ip.h>
-#include <linux/icmp.h>
-#include <arpa/inet.h>
-#include <stdlib.h>
-#include <netinet/if_ether.h>
-#include <net/if_arp.h>
-#include <asm/byteorder.h>
-#include <byteswap.h>
-#include <string.h>
-#include <sys/epoll.h>
-#include <errno.h>
-#include <unistd.h>
-#include <signal.h>
-
-#include <libmemif.h>
-#include <icmp_proto.h>
-
-#define APP_NAME "ICMP_Responder"
-#define IF_NAME "memif_connection"
-
-
-#ifdef ICMP_DBG
-#define DBG(...) do { \
- printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
- printf (__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-#else
-#define DBG(...)
-#endif
-
-#define INFO(...) do { \
- printf ("INFO: "__VA_ARGS__); \
- printf ("\n"); \
- } while (0)
-
-/* maximum tx/rx memif buffers */
-#define MAX_MEMIF_BUFS 256
-
-typedef struct
-{
- uint16_t index;
- /* memif conenction handle */
- memif_conn_handle_t conn;
- /* transmit queue id */
- uint16_t tx_qid;
- /* tx buffers */
- memif_buffer_t *tx_bufs;
- /* allocated tx buffers counter */
- /* number of tx buffers pointing to shared memory */
- uint16_t tx_buf_num;
- /* rx buffers */
- memif_buffer_t *rx_bufs;
- /* allcoated rx buffers counter */
- /* number of rx buffers pointing to shared memory */
- uint16_t rx_buf_num;
- /* interface ip address */
- uint8_t ip_addr[4];
-} memif_connection_t;
-
-memif_connection_t memif_connection;
-int epfd;
-
-static void
-print_memif_details ()
-{
- memif_connection_t *c = &memif_connection;
- printf ("MEMIF DETAILS\n");
- printf ("==============================\n");
-
-
- memif_details_t md;
- memset (&md, 0, sizeof (md));
- ssize_t buflen = 2048;
- char *buf = malloc (buflen);
- memset (buf, 0, buflen);
- int err, e;
-
- err = memif_get_details (c->conn, &md, buf, buflen);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("%s", memif_strerror (err));
- if (err == MEMIF_ERR_NOCONN)
- {
- free (buf);
- return;
- }
- }
-
- printf ("\tinterface name: %s\n", (char *) md.if_name);
- printf ("\tapp name: %s\n", (char *) md.inst_name);
- printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
- printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
- printf ("\tid: %u\n", md.id);
- printf ("\tsecret: %s\n", (char *) md.secret);
- printf ("\trole: ");
- if (md.role)
- printf ("slave\n");
- else
- printf ("master\n");
- printf ("\tmode: ");
- switch (md.mode)
- {
- case 0:
- printf ("ethernet\n");
- break;
- case 1:
- printf ("ip\n");
- break;
- case 2:
- printf ("punt/inject\n");
- break;
- default:
- printf ("unknown\n");
- break;
- }
- printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
- printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
- printf ("\trx queues:\n");
- for (e = 0; e < md.rx_queues_num; e++)
- {
- printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
- printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
- printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
- }
- printf ("\ttx queues:\n");
- for (e = 0; e < md.tx_queues_num; e++)
- {
- printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
- printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
- printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
- }
- printf ("\tlink: ");
- if (md.link_up_down)
- printf ("up\n");
- else
- printf ("down\n");
-
- free (buf);
-}
-
-/* informs user about connected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_connect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif connected!");
- return 0;
-}
-
-/* informs user about disconnected status. private_ctx is used by user to identify connection
- (multiple connections WIP) */
-int
-on_disconnect (memif_conn_handle_t conn, void *private_ctx)
-{
- INFO ("memif disconnected!");
- return 0;
-}
-
-int
-icmpr_memif_delete ()
-{
- int err;
- /* disconnect then delete memif connection */
- err = memif_delete (&(&memif_connection)->conn);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_delete: %s", memif_strerror (err));
- return 0;
-}
-
-void
-print_help ()
-{
- printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
-#ifdef ICMP_DBG
- printf (" (debug)");
-#endif
- printf ("\n");
- printf ("==============================\n");
- printf ("libmemif version: %s", LIBMEMIF_VERSION);
-#ifdef MEMIF_DBG
- printf (" (debug)");
-#endif
- printf ("\n");
- printf ("memif version: %d\n", memif_get_version ());
- printf ("\tuse CTRL+C to exit\n");
-}
-
-int
-icmpr_buffer_alloc (long n, uint16_t qid)
-{
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t r;
- /* set data pointer to shared memory and set buffer_len to shared memory buffer len */
- err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0);
- if (err != MEMIF_ERR_SUCCESS)
- {
- INFO ("memif_buffer_alloc: %s", memif_strerror (err));
- c->tx_buf_num += r;
- return -1;
- }
- c->tx_buf_num += r;
- DBG ("allocated %d/%ld buffers, %u free buffers", r, n,
- MAX_MEMIF_BUFS - c->tx_buf_num);
- return 0;
-}
-
-int
-icmpr_tx_burst (uint16_t qid)
-{
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t r;
- /* inform peer memif interface about data in shared memory buffers */
- /* mark memif buffers as free */
- err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_tx_burst: %s", memif_strerror (err));
- DBG ("tx: %d/%u", r, c->tx_buf_num);
- c->tx_buf_num -= r;
- return 0;
-}
-
-int
-icmpr_free ()
-{
- /* application cleanup */
- int err;
- memif_connection_t *c = &memif_connection;
- free (c->tx_bufs);
- c->tx_bufs = NULL;
- free (c->rx_bufs);
- c->rx_bufs = NULL;
-
- err = memif_cleanup ();
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_delete: %s", memif_strerror (err));
-
- return 0;
-}
-
-void
-icmpr_exit (int sig)
-{
- printf ("\n");
- icmpr_memif_delete ();
- icmpr_free ();
- exit (EXIT_SUCCESS);
-}
-
-/* called when event is polled on interrupt file descriptor.
- there are packets in shared memory ready to be received */
-int
-on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
-{
- DBG ("interrupted");
- memif_connection_t *c = &memif_connection;
- int err;
- uint16_t rx;
- /* receive data from shared memory buffers */
- err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
- c->rx_buf_num += rx;
-
- DBG ("received %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
-
- if (icmpr_buffer_alloc (rx, c->tx_qid) < 0)
- {
- INFO ("buffer_alloc error");
- goto error;
- }
- int i;
- for (i = 0; i < rx; i++)
- {
- resolve_packet ((void *) (c->rx_bufs + i)->data,
- (c->rx_bufs + i)->len,
- (void *) (c->tx_bufs + i)->data,
- &(c->tx_bufs + i)->len, c->ip_addr);
- }
-
- /* mark memif buffers and shared memory buffers as free */
- err = memif_refill_queue (c->conn, qid, rx, 0);
- /*
- * In this example we can assert that c->conn points to valid connection
- * and 'rx <= c->rx_buf_num'.
- */
- c->rx_buf_num -= rx;
-
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
-
- icmpr_tx_burst (c->tx_qid);
-
- return 0;
-
-error:
- err = memif_refill_queue (c->conn, qid, rx, 0);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_buffer_free: %s", memif_strerror (err));
- c->rx_buf_num -= rx;
- DBG ("freed %d buffers. %u/%u alloc/free buffers",
- rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
- return 0;
-}
-
-int
-icmpr_memif_create (int is_master)
-{
- /* setting memif connection arguments */
- memif_conn_args_t args;
- memset (&args, 0, sizeof (args));
- args.is_master = is_master;
- args.log2_ring_size = 10;
- args.buffer_size = 2048;
- args.num_s2m_rings = 2;
- args.num_m2s_rings = 2;
- strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
- args.mode = 0;
- /* socket filename is not specified, because this app is supposed to
- connect to VPP over memif. so default socket filename will be used */
- /* default socketfile = /run/vpp/memif.sock */
-
- args.interface_id = 0;
- /* last argument for memif_create (void * private_ctx) is used by user
- to identify connection. this context is returned with callbacks */
- int err = memif_create (&(&memif_connection)->conn,
- &args, on_connect, on_disconnect, on_interrupt,
- NULL);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_create: %s", memif_strerror (err));
- return 0;
-}
-
-int
-main (int argc, char *argv[])
-{
- memif_connection_t *c = &memif_connection;
-
- signal (SIGINT, icmpr_exit);
-
- /* initialize global memif connection handle */
- c->conn = NULL;
- if (argc == 1)
- c->tx_qid = 0;
- else
- {
- char *end;
- c->tx_qid = strtol (argv[1], &end, 10);
- }
- INFO ("tx qid: %u", c->tx_qid);
- /* alloc memif buffers */
- c->rx_buf_num = 0;
- c->rx_bufs =
- (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- c->tx_buf_num = 0;
- c->tx_bufs =
- (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
- c->ip_addr[0] = 192;
- c->ip_addr[1] = 168;
- c->ip_addr[2] = 1;
- c->ip_addr[3] = 2;
- /* initialize memory interface */
- int err;
- /* if valid callback is passed as argument, fd event polling will be done by user
- all file descriptors and events will be passed to user in this callback */
- /* if callback is set to NULL libmemif will handle fd event polling */
- err = memif_init (NULL, APP_NAME, NULL, NULL, NULL);
- if (err != MEMIF_ERR_SUCCESS)
- INFO ("memif_init: %s", memif_strerror (err));
-
- print_help ();
-
- icmpr_memif_create (0);
- print_memif_details ();
-
- /* main loop */
- while (1)
- {
- if (memif_poll_event (-1) < 0)
- {
- DBG ("poll_event error!");
- }
- }
-}