DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ARP.cpp
Go to the documentation of this file.
1
8#include <arpa/inet.h>
9#include <rte_arp.h>
10#include <rte_ethdev.h>
11
12#include <iostream>
13#include <sstream>
14#include <iomanip>
15
16#include "dpdklibs/arp/ARP.hpp"
17
18namespace dunedaq {
19namespace dpdklibs {
20namespace arp {
21
22void
23pktgen_send_garp(struct rte_mbuf *m, uint32_t port_id, rte_be32_t ip_add_bin)
24{
25 struct rte_ether_hdr *eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
26 struct rte_arp_hdr *arp = (struct rte_arp_hdr *)&eth[1];
27
28 /* src and dest addr */
29 memset(&eth->dst_addr, 0xFF, 6);
30 // MAC addr of port
31 struct rte_ether_addr mac_addr;
32 rte_eth_macaddr_get(port_id, &mac_addr);
33 rte_ether_addr_copy(&mac_addr, &eth->src_addr);
34 // Set ETH type
35 eth->ether_type = htons(RTE_ETHER_TYPE_ARP);
36
37
38 memset(arp, 0, sizeof(struct rte_arp_hdr));
39 rte_memcpy(&arp->arp_data.arp_sha, &mac_addr, 6);
40
41 uint32_t addr = htonl(ip_add_bin);
42 inetAddrCopy(&arp->arp_data.arp_sip, &addr);
43
44 //if (likely(type == GRATUITOUS_ARP) ) {
45 rte_memcpy(&arp->arp_data.arp_tha, &mac_addr, 6);
46 inetAddrCopy(&arp->arp_data.arp_tip, &addr);
47 //} else {
48 // memset(&arp->arp_data.arp_tha, 0, 6);
49 // addr = htonl(pkt->ip_dst_addr.addr.ipv4.s_addr);
50 // inetAddrCopy(&arp->arp_data.arp_tip, &addr);
51 //}
52
53 /* Fill in the rest of the ARP packet header */
54 arp->arp_hardware = htons(RTE_ARP_HRD_ETHER);
55 arp->arp_protocol = htons(RTE_ETHER_TYPE_IPV4);
56 arp->arp_hlen = 6;
57 arp->arp_plen = 4;
58 arp->arp_opcode = htons(RTE_ARP_OP_REQUEST);
59
60 m->pkt_len = 60;
61 m->data_len = 60;
62
63 struct rte_mbuf *arp_tx_mbuf[1];
64 arp_tx_mbuf[0] = m;
65 rte_eth_tx_burst(port_id, 0, arp_tx_mbuf, 1);
66 //printf("Sending ARP reply\n");
67}
68
69
70inline void
71hex_digits_to_stream(std::ostringstream& ostrs, int value, char separator = ':', char fill = '0', int digits = 2) {
72 ostrs << std::setfill(fill) << std::setw(digits) << std::hex << value << std::dec << separator;
73}
74
75
76void
77pktgen_process_arp(struct rte_mbuf *m, uint32_t port_id, rte_be32_t ip_add_bin)
78{
79 struct rte_ether_hdr *eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
80 struct rte_arp_hdr *arp = (struct rte_arp_hdr *)&eth[1];
81
82 if (arp->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
83 arp->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
84
85
86 /* Grab the source MAC addresses */
87 struct rte_ether_addr mac_addr;
88 rte_eth_macaddr_get(port_id, &mac_addr);
89
90 std::string srcaddr = dunedaq::dpdklibs::udp::get_ipv4_decimal_addr_str(dunedaq::dpdklibs::udp::ip_address_binary_to_dotdecimal(rte_be_to_cpu_32(arp->arp_data.arp_sip)));
91 TLOG_DEBUG(10) << "SRC IP: " << srcaddr;
92 std::string dstaddr = dunedaq::dpdklibs::udp::get_ipv4_decimal_addr_str(dunedaq::dpdklibs::udp::ip_address_binary_to_dotdecimal(rte_be_to_cpu_32(arp->arp_data.arp_tip)));
93 TLOG_DEBUG(10) << "DEST IP: " << dstaddr;
95 TLOG_DEBUG(10) << "LOCAL IP: " << localaddr;
96
97 // Bail out if not our ipaddress
98 if ( arp->arp_data.arp_tip != ip_add_bin) return;
99
100 TLOG_DEBUG(10) << "ARP Received " << dstaddr << " I'm the target " << localaddr;
101
102 /* Swap the two MAC addresses */
103 ethAddrSwap(&arp->arp_data.arp_sha, &arp->arp_data.arp_tha);
104
105 /* Swap the two IP addresses */
106 inetAddrSwap(&arp->arp_data.arp_tip, &arp->arp_data.arp_sip);
107
108 /* Set the packet to ARP reply */
109 arp->arp_opcode = htons(RTE_ARP_OP_REPLY);
110
111 /* Swap the MAC addresses */
112 ethAddrSwap(&eth->dst_addr, &eth->src_addr);
113
114 /* Copy in the MAC address for the reply. */
115 rte_memcpy(&arp->arp_data.arp_sha, &mac_addr, 6);
116 rte_memcpy(&eth->src_addr, &mac_addr, 6);
117
118 struct rte_mbuf *arp_tx_mbuf[1];
119 arp_tx_mbuf[0] = m;
120
121 rte_eth_tx_burst(port_id, 0, arp_tx_mbuf, 1);
122 TLOG_DEBUG(10) << "Sending ARP reply";
123
124 /* No need to free mbuf as it was reused */
125 return;
126 }
127}
128
129} // namespace arp
130} // namespace dpdklibs
131} // namespace dunedaq
static void fill(const ResourceSet &rs, std::vector< const ResourceSetOR * > &rs_or, std::vector< const ResourceSetAND * > &rs_and, TestCircularDependency &cd_fuse)
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
void inetAddrCopy(void *t, void *f)
Definition ARP.hpp:68
void ethAddrSwap(void *t, void *f)
Definition ARP.hpp:47
void pktgen_send_garp(struct rte_mbuf *m, uint32_t port_id, rte_be32_t binary_ip_address)
Definition ARP.cpp:23
void hex_digits_to_stream(std::ostringstream &ostrs, int value, char separator=':', char fill='0', int digits=2)
Definition ARP.cpp:71
void pktgen_process_arp(struct rte_mbuf *m, uint32_t pid, rte_be32_t binary_ip_address)
Definition ARP.cpp:77
void inetAddrSwap(void *t, void *f)
Definition ARP.hpp:57
std::string get_ipv4_decimal_addr_str(struct ipaddr ipv4_address)
Definition Utils.cpp:56
struct ipaddr ip_address_binary_to_dotdecimal(rte_le32_t binary_ipv4_address)
Definition Utils.cpp:48
Including Qt Headers.