ethertap.hh (12054:ab04045965d1) ethertap.hh (12055:945e851d846b)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 33 unchanged lines hidden (view full) ---

42#include "dev/net/etherint.hh"
43#include "dev/net/etherobject.hh"
44#include "dev/net/etherpkt.hh"
45#include "params/EtherTapStub.hh"
46#include "sim/eventq.hh"
47#include "sim/sim_object.hh"
48
49class TapEvent;
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 33 unchanged lines hidden (view full) ---

42#include "dev/net/etherint.hh"
43#include "dev/net/etherobject.hh"
44#include "dev/net/etherpkt.hh"
45#include "params/EtherTapStub.hh"
46#include "sim/eventq.hh"
47#include "sim/sim_object.hh"
48
49class TapEvent;
50class TapListener;
51class EtherTapInt;
52
50class EtherTapInt;
51
53/*
54 * Interface to connect a simulated ethernet device to the real world. An
55 * external helper program bridges between this object's TCP port and a
56 * source/sink for Ethernet frames. Each frame going in either direction is
57 * prepended with the frame's length in a 32 bit integer in network byte order.
58 */
59class EtherTapStub : public EtherObject
52class EtherTapBase : public EtherObject
60{
53{
61 protected:
62 friend class TapEvent;
63 TapEvent *event;
54 public:
55 typedef EtherTapBaseParams Params;
56 EtherTapBase(const Params *p);
57 virtual ~EtherTapBase();
64
58
59 const Params *
60 params() const
61 {
62 return dynamic_cast<const Params *>(_params);
63 }
64
65 void serialize(CheckpointOut &cp) const override;
66 void unserialize(CheckpointIn &cp) override;
67
65 protected:
68 protected:
66 friend class TapListener;
67 TapListener *listener;
68 int socket;
69 char *buffer;
69 uint8_t *buffer;
70 int buflen;
70 int buflen;
71 uint32_t buffer_offset;
72 uint32_t data_len;
73
74 EtherDump *dump;
75
71
72 EtherDump *dump;
73
76 void attach(int fd);
77 void detach();
78
74
75 /*
76 * Interface to the real network.
77 */
79 protected:
78 protected:
80 std::string device;
81 std::queue<EthPacketPtr> packetBuffer;
82 EtherTapInt *interface;
79 friend class TapEvent;
80 TapEvent *event;
81 void pollFd(int fd);
82 void stopPolling();
83
83
84 void process(int revent);
85 void enqueue(EthPacketData *packet);
86 void retransmit();
84 // Receive data from the real network.
85 virtual void recvReal(int revent) = 0;
86 // Prepare and send data out to the real network.
87 virtual bool sendReal(const void *data, size_t len) = 0;
87
88
89
88 /*
90 /*
91 * Interface to the simulated network.
89 */
92 */
93 protected:
94 EtherTapInt *interface;
95
96 public:
97 EtherInt *getEthPort(const std::string &if_name, int idx) override;
98
99 bool recvSimulated(EthPacketPtr packet);
100 void sendSimulated(void *data, size_t len);
101
102 protected:
103 std::queue<EthPacketPtr> packetBuffer;
104 void retransmit();
105
90 class TxEvent : public Event
91 {
92 protected:
106 class TxEvent : public Event
107 {
108 protected:
93 EtherTapStub *tap;
109 EtherTapBase *tap;
94
95 public:
110
111 public:
96 TxEvent(EtherTapStub *_tap) : tap(_tap) {}
112 TxEvent(EtherTapBase *_tap) : tap(_tap) {}
97 void process() { tap->retransmit(); }
98 virtual const char *description() const
113 void process() { tap->retransmit(); }
114 virtual const char *description() const
99 { return "EtherTapStub retransmit"; }
115 { return "EtherTapBase retransmit"; }
100 };
101
102 friend class TxEvent;
103 TxEvent txEvent;
116 };
117
118 friend class TxEvent;
119 TxEvent txEvent;
120};
104
121
122class EtherTapInt : public EtherInt
123{
124 private:
125 EtherTapBase *tap;
105 public:
126 public:
127 EtherTapInt(const std::string &name, EtherTapBase *t) :
128 EtherInt(name), tap(t)
129 { }
130
131 bool recvPacket(EthPacketPtr pkt) override
132 { return tap->recvSimulated(pkt); }
133 void sendDone() override {}
134};
135
136
137class TapListener;
138
139/*
140 * Interface to connect a simulated ethernet device to the real world. An
141 * external helper program bridges between this object's TCP port and a
142 * source/sink for Ethernet frames. Each frame going in either direction is
143 * prepended with the frame's length in a 32 bit integer in network byte order.
144 */
145class EtherTapStub : public EtherTapBase
146{
147 public:
106 typedef EtherTapStubParams Params;
107 EtherTapStub(const Params *p);
148 typedef EtherTapStubParams Params;
149 EtherTapStub(const Params *p);
108 virtual ~EtherTapStub();
150 ~EtherTapStub();
109
110 const Params *
111 params() const
112 {
113 return dynamic_cast<const Params *>(_params);
114 }
115
151
152 const Params *
153 params() const
154 {
155 return dynamic_cast<const Params *>(_params);
156 }
157
116 EtherInt *getEthPort(const std::string &if_name, int idx) override;
117
118 virtual bool recvPacket(EthPacketPtr packet);
119 virtual void sendDone();
120
121 void serialize(CheckpointOut &cp) const override;
122 void unserialize(CheckpointIn &cp) override;
158 void serialize(CheckpointOut &cp) const override;
159 void unserialize(CheckpointIn &cp) override;
123};
124
160
125class EtherTapInt : public EtherInt
126{
127 private:
128 EtherTapStub *tap;
129 public:
130 EtherTapInt(const std::string &name, EtherTapStub *t)
131 : EtherInt(name), tap(t)
132 { }
133
161
134 virtual bool recvPacket(EthPacketPtr pkt) { return tap->recvPacket(pkt); }
135 virtual void sendDone() { tap->sendDone(); }
162 protected:
163 friend class TapListener;
164 TapListener *listener;
165
166 int socket;
167
168 void attach(int fd);
169 void detach();
170
171 uint32_t buffer_used;
172 uint32_t frame_len;
173
174 void recvReal(int revent) override;
175 bool sendReal(const void *data, size_t len) override;
136};
137
138
139#endif // __DEV_NET_ETHERTAP_HH__
176};
177
178
179#endif // __DEV_NET_ETHERTAP_HH__