Seastar
High performance C++ framework for concurrent servers
ethernet.hh
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2014 Cloudius Systems, Ltd.
20  */
21 
22 #pragma once
23 
24 #if FMT_VERSION >= 90000
25 #include <fmt/ostream.h>
26 #endif
27 
28 #include <array>
29 #include <assert.h>
30 #include <algorithm>
31 #include <seastar/net/byteorder.hh>
32 
33 namespace seastar {
34 
35 namespace net {
36 
38  ethernet_address() noexcept
39  : mac{} {}
40 
41  ethernet_address(const uint8_t *eaddr) noexcept {
42  std::copy(eaddr, eaddr + 6, mac.begin());
43  }
44 
45  ethernet_address(std::initializer_list<uint8_t> eaddr) noexcept {
46  assert(eaddr.size() == mac.size());
47  std::copy(eaddr.begin(), eaddr.end(), mac.begin());
48  }
49 
50  std::array<uint8_t, 6> mac;
51 
52  template <typename Adjuster>
53  void adjust_endianness(Adjuster) noexcept {}
54 
55  static ethernet_address read(const char* p) noexcept {
57  std::copy_n(p, size(), reinterpret_cast<char*>(ea.mac.data()));\
58  return ea;
59  }
60  static ethernet_address consume(const char*& p) noexcept {
61  auto ea = read(p);
62  p += size();
63  return ea;
64  }
65  void write(char* p) const noexcept {
66  std::copy_n(reinterpret_cast<const char*>(mac.data()), size(), p);
67  }
68  void produce(char*& p) const noexcept {
69  write(p);
70  p += size();
71  }
72  static constexpr size_t size() noexcept {
73  return 6;
74  }
75 } __attribute__((packed));
76 
77 std::ostream& operator<<(std::ostream& os, ethernet_address ea);
78 
79 struct ethernet {
80  using address = ethernet_address;
81  static address broadcast_address() {
82  return {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
83  }
84  static constexpr uint16_t arp_hardware_type() { return 1; }
85 };
86 
87 struct eth_hdr {
88  ethernet_address dst_mac;
89  ethernet_address src_mac;
90  packed<uint16_t> eth_proto;
91  template <typename Adjuster>
92  auto adjust_endianness(Adjuster a) {
93  return a(eth_proto);
94  }
95 } __attribute__((packed));
96 
97 ethernet_address parse_ethernet_address(std::string addr);
98 }
99 
100 }
101 
102 #if FMT_VERSION >= 90000
103 template <> struct fmt::formatter<seastar::net::ethernet_address> : fmt::ostream_formatter {};
104 #endif
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: ethernet.hh:87
Definition: ethernet.hh:37
Definition: ethernet.hh:79
Definition: unaligned.hh:58