Skip to content

Commit

Permalink
icmp: factorize init code
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Jarry <[email protected]>
  • Loading branch information
rjarry committed Apr 17, 2024
1 parent e3f3a8c commit 876f300
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
17 changes: 2 additions & 15 deletions modules/ip4/datapath/icmp_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <br_log.h>
#include <br_mbuf.h>

#include <rte_byteorder.h>
#include <rte_ether.h>
#include <rte_graph_worker.h>
#include <rte_icmp.h>
#include <rte_ip.h>
Expand All @@ -22,8 +20,6 @@ enum {
EDGE_COUNT,
};

#define IPV4_VERSION_IHL 0x45

static uint16_t
icmp_output_process(struct rte_graph *graph, struct rte_node *node, void **objs, uint16_t nb_objs) {
struct ip_local_mbuf_data *local_data;
Expand All @@ -33,23 +29,14 @@ icmp_output_process(struct rte_graph *graph, struct rte_node *node, void **objs,

for (uint16_t i = 0; i < nb_objs; i++) {
mbuf = objs[i];
icmp = rte_pktmbuf_mtod(mbuf, struct rte_icmp_hdr *);
local_data = ip_local_mbuf_data(mbuf);

icmp = rte_pktmbuf_mtod(mbuf, struct rte_icmp_hdr *);
icmp->icmp_cksum = 0;
icmp->icmp_cksum = ~rte_raw_cksum(icmp, local_data->len);

ip = (struct rte_ipv4_hdr *)rte_pktmbuf_prepend(mbuf, sizeof(*ip));

memset(ip, 0, sizeof(*ip));
ip->version_ihl = IPV4_VERSION_IHL;
ip->total_length = rte_cpu_to_be_16(local_data->len + sizeof(*ip));
ip->time_to_live = 64;
ip->next_proto_id = IPPROTO_ICMP;
ip->src_addr = local_data->src;
ip->dst_addr = local_data->dst;
ip->hdr_checksum = rte_ipv4_cksum(ip);

ip4_set_fields(ip, local_data);
ip_output_mbuf_data(mbuf)->nh = NULL;
}

Expand Down
19 changes: 19 additions & 0 deletions modules/ip4/datapath/ip4.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <br_mbuf.h>
#include <br_net_types.h>

#include <rte_byteorder.h>
#include <rte_graph_worker.h>
#include <rte_ip.h>

#include <stdint.h>

Expand All @@ -27,4 +29,21 @@ BR_MBUF_PRIV_DATA_TYPE(ip_local_mbuf_data, {

void ip4_local_add_proto(uint8_t proto, rte_edge_t edge);

#define IPV4_VERSION_IHL 0x45
#define IPV4_DEFAULT_TTL 64

static inline void ip4_set_fields(struct rte_ipv4_hdr *ip, struct ip_local_mbuf_data *data) {
ip->version_ihl = IPV4_VERSION_IHL;
ip->type_of_service = 0;
ip->total_length = rte_cpu_to_be_16(data->len + sizeof(*ip));
ip->fragment_offset = 0;
ip->packet_id = 0;
ip->time_to_live = IPV4_DEFAULT_TTL; // make this confgurable somehow?
ip->next_proto_id = data->proto;
ip->src_addr = data->src;
ip->dst_addr = data->dst;
ip->hdr_checksum = 0;
ip->hdr_checksum = rte_ipv4_cksum(ip);
}

#endif

0 comments on commit 876f300

Please sign in to comment.