|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2017 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 | + |
| 18 | +#include <hw/nic.hpp> |
| 19 | + |
| 20 | +#ifndef NICK_MOCK_HPP |
| 21 | +#define NICK_MOCK_HPP |
| 22 | + |
| 23 | +#define NIC_INFO(X,...) INFO("Mock NIC", X, ##__VA_ARGS__) |
| 24 | + |
| 25 | +class Nic_mock : public hw::Nic { |
| 26 | + |
| 27 | +public: |
| 28 | + Proto proto() const override |
| 29 | + { return Nic::Proto::ETH; } |
| 30 | + |
| 31 | + const char* driver_name() const override |
| 32 | + { return "Mock driver"; } |
| 33 | + |
| 34 | + /** The mac address. */ |
| 35 | + const MAC::Addr& mac() const noexcept override |
| 36 | + { return mac_; } |
| 37 | + |
| 38 | + virtual uint16_t MTU() const noexcept override |
| 39 | + { return 1500; } |
| 40 | + |
| 41 | + |
| 42 | + downstream create_link_downstream() override |
| 43 | + { return transmit_to_link_; }; |
| 44 | + |
| 45 | + void set_ip4_upstream(upstream handler) override |
| 46 | + { ip4_handler_ = handler; } |
| 47 | + |
| 48 | + void set_ip6_upstream(upstream handler) override |
| 49 | + { ip6_handler_ = handler; } |
| 50 | + |
| 51 | + void set_arp_upstream(upstream handler) override |
| 52 | + { arp_handler_ = handler; } |
| 53 | + |
| 54 | + /** Number of bytes in a frame needed by the device itself **/ |
| 55 | + size_t frame_offset_device() override |
| 56 | + { return frame_offs_dev_; } |
| 57 | + |
| 58 | + /** Number of bytes in a frame needed by the link layer **/ |
| 59 | + size_t frame_offset_link() override |
| 60 | + { return frame_offs_link_; } |
| 61 | + |
| 62 | + static constexpr uint16_t packet_len() |
| 63 | + { return 1514; } |
| 64 | + |
| 65 | + net::Packet_ptr create_packet(int) override |
| 66 | + { |
| 67 | + auto buffer = bufstore().get_buffer(); |
| 68 | + auto* ptr = (net::Packet*) buffer.addr; |
| 69 | + |
| 70 | + new (ptr) net::Packet(frame_offs_dev_ + frame_offs_link_, |
| 71 | + 0, |
| 72 | + frame_offs_dev_ + packet_len(), |
| 73 | + buffer.bufstore); |
| 74 | + |
| 75 | + return net::Packet_ptr(ptr); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + size_t transmit_queue_available() override |
| 80 | + { return 1024; } |
| 81 | + |
| 82 | + void deactivate() override |
| 83 | + { link_up_ = false; } |
| 84 | + |
| 85 | + /** Stats getters **/ |
| 86 | + uint64_t get_packets_rx() override |
| 87 | + { return packets_rx_; } |
| 88 | + |
| 89 | + uint64_t get_packets_tx() override |
| 90 | + { return packets_tx_; } |
| 91 | + |
| 92 | + uint64_t get_packets_dropped() override |
| 93 | + { return packets_dropped_; } |
| 94 | + |
| 95 | + void move_to_this_cpu() override {} |
| 96 | + |
| 97 | + // |
| 98 | + // MOCK Extensions |
| 99 | + // |
| 100 | + |
| 101 | + ~Nic_mock() {} |
| 102 | + Nic_mock() : Nic(256, 2048) {} |
| 103 | + |
| 104 | + // Public data members (ahem) |
| 105 | + MAC::Addr mac_ = {0xc0,0x00,0x01,0x70,0x00,0x01}; |
| 106 | + static constexpr size_t frame_offs_dev_ = 0; |
| 107 | + static constexpr size_t frame_offs_link_ = 14; |
| 108 | + |
| 109 | + std::vector<net::Packet_ptr> tx_queue_; |
| 110 | + |
| 111 | + void transmit(net::Packet_ptr, MAC::Addr, net::Ethertype) |
| 112 | + { |
| 113 | + NIC_INFO("transimtting packet"); |
| 114 | + //tx_queue_.emplace_back(std::move(ptr)); |
| 115 | + } |
| 116 | + |
| 117 | + void receive(net::Packet_ptr ptr){ |
| 118 | + |
| 119 | + if(ip4_handler_) { |
| 120 | + NIC_INFO("pushing packet to IP4"); |
| 121 | + ip4_handler_(std::move(ptr)); |
| 122 | + } else { |
| 123 | + NIC_INFO("nowhere to push packet. Drop."); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +private: |
| 128 | + |
| 129 | + upstream ip4_handler_ = nullptr; |
| 130 | + upstream ip6_handler_ = nullptr; |
| 131 | + upstream arp_handler_ = nullptr; |
| 132 | + downstream transmit_to_link_ = downstream{this, &Nic_mock::transmit}; |
| 133 | + bool link_up_ = true; |
| 134 | + uint64_t packets_rx_ = 0; |
| 135 | + uint64_t packets_tx_ = 0; |
| 136 | + uint64_t packets_dropped_ = 0; |
| 137 | + |
| 138 | + |
| 139 | +}; |
| 140 | + |
| 141 | + |
| 142 | +#endif |
0 commit comments