Skip to content

Commit 1664c2e

Browse files
authored
Merge pull request #857 from AndreasAakesson/dev
net: Work on isolation/layering - Ethernet & IP4
2 parents 60012e8 + de129df commit 1664c2e

18 files changed

Lines changed: 329 additions & 217 deletions

File tree

api/hw/mac_addr.hpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
#pragma once
18+
19+
#ifndef HW_MAC_ADDR_HPP
20+
#define HW_MAC_ADDR_HPP
21+
22+
namespace hw {
23+
// MAC address
24+
union MAC_addr {
25+
// no. parts in a MAC address
26+
static constexpr size_t PARTS_LEN = 6;
27+
// The parts of the MAC address
28+
uint8_t part[PARTS_LEN];
29+
30+
struct {
31+
uint16_t minor;
32+
uint32_t major;
33+
} __attribute__((packed));
34+
35+
MAC_addr() noexcept : part{} {}
36+
37+
MAC_addr(const uint8_t a, const uint8_t b, const uint8_t c,
38+
const uint8_t d, const uint8_t e, const uint8_t f) noexcept
39+
: part{a,b,c,d,e,f}
40+
{}
41+
42+
MAC_addr& operator=(const MAC_addr cpy) noexcept {
43+
minor = cpy.minor;
44+
major = cpy.major;
45+
return *this;
46+
}
47+
48+
/**
49+
* @brief Hex representation of a MAC address
50+
*
51+
* @return hex string representation
52+
*/
53+
std::string hex_str() const {
54+
char hex_addr[18];
55+
snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x",
56+
part[0], part[1], part[2],
57+
part[3], part[4], part[5]);
58+
return hex_addr;
59+
}
60+
61+
/**
62+
* @brief String representation of a MAC address
63+
* @note default is hex
64+
* @return string representation of the MAC address
65+
*/
66+
std::string str() const
67+
{ return hex_str(); }
68+
69+
std::string to_string() const
70+
{ return str(); }
71+
72+
operator std::string () const
73+
{ return str(); }
74+
75+
/** Check for equality */
76+
bool operator==(const MAC_addr mac) const noexcept
77+
{
78+
return strncmp(
79+
reinterpret_cast<const char*>(part),
80+
reinterpret_cast<const char*>(mac.part),
81+
PARTS_LEN) == 0;
82+
}
83+
84+
} __attribute__((packed)); //< union addr
85+
}
86+
87+
#endif

api/hw/nic.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#include "../net/inet_common.hpp"
2424
#include "../net/buffer_store.hpp"
25-
#include "../net/ethernet.hpp"
25+
#include "mac_addr.hpp"
26+
#include <net/ethernet/header.hpp>
2627

2728
namespace hw {
2829

@@ -43,9 +44,8 @@ namespace hw {
4344

4445
/**
4546
The mac address.
46-
@todo remove depedency for Ethernet (somewhere in the future)
4747
*/
48-
virtual const net::Ethernet::addr& mac() = 0;
48+
virtual const MAC_addr& mac() = 0;
4949

5050
virtual uint16_t MTU() const noexcept = 0;
5151

@@ -58,8 +58,11 @@ namespace hw {
5858
uint16_t bufsize() const
5959
{ return bufstore_.bufsize(); }
6060

61+
/**
62+
* @todo remove depedency for Ethernet (somewhere in the future)
63+
*/
6164
uint16_t eth_size() const
62-
{ return sizeof(net::Ethernet::header) + sizeof(net::Ethernet::trailer); }
65+
{ return sizeof(net::ethernet::Header) + sizeof(net::ethernet::trailer_t); }
6366

6467

6568
/** Delegate linklayer output. Hooks into IP-stack bottom, w.UPSTREAM data. */

api/net/dhcp/dh4client.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <timers>
2323
#include "../packet.hpp"
24+
#include <net/ip4/ip4.hpp>
2425

2526
namespace net
2627
{
Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@
2121

2222
#include <string>
2323

24+
#include "header.hpp"
25+
#include <hw/nic.hpp>
26+
#include <hw/mac_addr.hpp>
2427
#include <net/inet_common.hpp>
2528

26-
namespace hw {
27-
class Nic;
28-
}
29-
3029
namespace net {
3130

3231
/** Ethernet packet handling. */
3332
class Ethernet {
3433
public:
35-
static constexpr size_t ETHER_ADDR_LEN = 6;
3634
static constexpr size_t MINIMUM_PAYLOAD = 46;
3735

3836
/**
@@ -61,64 +59,19 @@ namespace net {
6159
};
6260

6361
// MAC address
64-
union addr {
65-
uint8_t part[ETHER_ADDR_LEN];
66-
67-
struct {
68-
uint16_t minor;
69-
uint32_t major;
70-
} __attribute__((packed));
71-
72-
addr() noexcept : part{} {}
73-
74-
addr(const uint8_t a, const uint8_t b, const uint8_t c,
75-
const uint8_t d, const uint8_t e, const uint8_t f) noexcept
76-
: part{a,b,c,d,e,f}
77-
{}
78-
79-
addr& operator=(const addr cpy) noexcept {
80-
minor = cpy.minor;
81-
major = cpy.major;
82-
return *this;
83-
}
84-
85-
// hex string representation
86-
std::string str() const {
87-
char eth_addr[18];
88-
snprintf(eth_addr, sizeof(eth_addr), "%02x:%02x:%02x:%02x:%02x:%02x",
89-
part[0], part[1], part[2],
90-
part[3], part[4], part[5]);
91-
return eth_addr;
92-
}
93-
94-
/** Check for equality */
95-
bool operator==(const addr mac) const noexcept
96-
{
97-
return strncmp(
98-
reinterpret_cast<const char*>(part),
99-
reinterpret_cast<const char*>(mac.part),
100-
ETHER_ADDR_LEN) == 0;
101-
}
102-
103-
static const addr MULTICAST_FRAME;
104-
static const addr BROADCAST_FRAME;
105-
106-
static const addr IPv6mcast_01;
107-
static const addr IPv6mcast_02;
108-
109-
} __attribute__((packed)); //< union addr
62+
using addr = hw::MAC_addr;
11063

111-
/** Constructor */
112-
explicit Ethernet(hw::Nic& nic) noexcept;
64+
static const addr MULTICAST_FRAME;
65+
static const addr BROADCAST_FRAME;
11366

114-
struct header {
115-
addr dest;
116-
addr src;
117-
unsigned short type;
67+
static const addr IPv6mcast_01;
68+
static const addr IPv6mcast_02;
11869

119-
} __attribute__((packed)) ;
70+
/** Constructor */
71+
explicit Ethernet(hw::Nic& nic) noexcept;
12072

121-
using trailer = uint32_t;
73+
using header = ethernet::Header;
74+
using trailer = ethernet::trailer_t;
12275

12376
/** Bottom upstream input, "Bottom up". Handle raw ethernet buffer. */
12477
void bottom(Packet_ptr);
@@ -147,7 +100,8 @@ namespace net {
147100
{ physical_out_ = del; }
148101

149102
/** @return Mac address of the underlying device */
150-
const addr mac() const noexcept;
103+
const auto& mac() const noexcept
104+
{ return nic_.mac(); }
151105

152106
/** Transmit data, with preallocated space for eth.header */
153107
void transmit(Packet_ptr);

api/net/ethernet/header.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
#pragma once
18+
19+
#ifndef NET_ETHERNET_HEADER_HPP
20+
#define NET_ETHERNET_HEADER_HPP
21+
22+
#include <hw/mac_addr.hpp>
23+
24+
namespace net {
25+
namespace ethernet {
26+
27+
using trailer_t = uint32_t;
28+
29+
struct Header {
30+
hw::MAC_addr dest;
31+
hw::MAC_addr src;
32+
unsigned short type;
33+
34+
} __attribute__((packed)) ;
35+
36+
}
37+
}
38+
39+
#endif

api/net/inet4.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <hw/devices.hpp> // 107: auto& eth0 = Dev::eth(0);
2323
#include <hw/nic.hpp>
2424
#include "inet.hpp"
25-
#include "ethernet.hpp"
25+
#include "ethernet/ethernet.hpp"
2626
#include "ip4/arp.hpp"
2727
#include "ip4/ip4.hpp"
2828
#include "ip4/udp.hpp"

api/net/ip4/header.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
#pragma once
18+
19+
#ifndef NET_IP4_HEADER_HPP
20+
#define NET_IP4_HEADER_HPP
21+
22+
namespace net {
23+
namespace ip4 {
24+
25+
/** IP4 header representation */
26+
struct Header {
27+
uint8_t version_ihl;
28+
uint8_t tos;
29+
uint16_t tot_len;
30+
uint16_t id;
31+
uint16_t frag_off_flags;
32+
uint8_t ttl;
33+
uint8_t protocol;
34+
uint16_t check;
35+
Addr saddr;
36+
Addr daddr;
37+
};
38+
39+
}
40+
}
41+
42+
#endif
43+

0 commit comments

Comments
 (0)