perf: Shutdown profiler/sampling on kpctl close
[akaros.git] / tools / packetizer / packetizer.h
1 #ifndef _PACKETIZER_H
2 #define _PACKETIZER_H
3
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <string.h>
7 #include <netpacket/packet.h>
8
9 #define PACKETIZER_ETHERTYPE 0xabcd
10 #define MAX_PAYLOAD_SIZE 1024
11 #define MAX_PACKET_SIZE (MAX_PAYLOAD_SIZE+sizeof(packet_header))
12 struct packet_header
13 {
14         uint8_t dst_mac[6];
15         uint8_t src_mac[6];
16         uint16_t ethertype;
17         uint16_t seqno;
18         uint32_t payload_size;
19 };
20
21 struct packet
22 {
23         packet_header header;
24         char payload[MAX_PAYLOAD_SIZE];
25         uint32_t packet_size;
26
27         uint32_t size()
28         {
29           return packet_size;
30         }
31
32         packet() {}
33         packet(const char* dst_mac, const char* src_mac, 
34                char seqno,int payload_size, const uint8_t* bytes)
35         {
36           header.ethertype = htons(PACKETIZER_ETHERTYPE);
37           memcpy(header.dst_mac,dst_mac,6);
38           memcpy(header.src_mac,src_mac,6);
39           header.seqno = htons(seqno);
40           header.payload_size = ntohl(payload_size);
41           if(bytes)
42             memcpy(payload,bytes,payload_size);
43           packet_size = sizeof(header)+payload_size;
44         }
45 };
46
47 class packetizer
48 {
49 public:
50         
51         packetizer(const char *target_mac, const char *eth_device, 
52                    const char *filename);
53         int start(void); 
54
55 protected:
56
57         sockaddr_ll myaddr;
58         int sock;
59         char host_mac[6];
60         char target_mac[6];
61         char eth_device[64];
62         char filename[256];
63
64         void send_packet(packet* packet);
65
66         uint16_t seqno;
67         uint16_t next_seqno() { return seqno++; }
68 };
69
70 #endif // _PACKETIZER_H