hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
Misc.h
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include <net/ethernet.h>
5 #include <netinet/in.h>
6 #include <netinet/ip.h>
7 #include <netinet/udp.h>
8 
9 namespace hmbdc { namespace comm { namespace eth {
10 inline
11 uint32_t
12 checksum(const void *data, uint16_t len, uint32_t sum) {
13  auto addr = (const uint8_t *)data;
14  uint32_t i;
15 
16  /* Checksum all the pairs of bytes first... */
17  for (i = 0; i < (len & ~1U); i += 2) {
18  sum += (u_int16_t)ntohs(*((u_int16_t *)(addr + i)));
19  if (sum > 0xFFFF)
20  sum -= 0xFFFF;
21  }
22  /*
23  * If there's a single byte left over, checksum it, too.
24  * Network byte order is big-endian, so the remaining byte is
25  * the high byte.
26  */
27  if (i < len) {
28  sum += addr[i] << 8;
29  if (sum > 0xFFFF)
30  sum -= 0xFFFF;
31  }
32  return sum;
33 }
34 
35 inline
36 uint16_t
37 cksum_add(uint16_t sum, uint16_t a) {
38  uint16_t res;
39 
40  res = sum + a;
41  return (res + (res < a));
42 }
43 
44 inline
45 uint16_t
46 wrapsum(uint32_t sum) {
47  sum = ~sum & 0xFFFF;
48  return (htons(sum));
49 }
50 
51 struct virt_header {
52  uint8_t fields[12];
53 } __attribute__((__packed__));
54 
55 struct pkt {
56  struct virt_header vh;
57  struct ether_header eh;
58  struct {
59  struct ip ip;
60  struct udphdr udp;
61  uint8_t body[1];
62  } ipv4;
63 } __attribute__((__packed__));
64 
65 // template<int s> struct Wow;
66 // Wow<sizeof(pkt)> wow;
67 static_assert(sizeof(pkt) == 58u, "wierd that size is not 58?");
68 
69 template <size_t N>
70 struct pkt_n {
71  struct virt_header vh;
72  struct ether_header eh;
73  struct {
74  struct ip ip;
75  struct udphdr udp;
76  uint8_t body[N];
77  } ipv4;
78 
79  operator pkt&() {
80  return reinterpret_cast<pkt&>(*this);
81  }
82 
83 } __attribute__((__packed__));
84 
85 }}}
Definition: Misc.h:55
Definition: Misc.h:51
Definition: Misc.h:70