|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2019 [email protected]
4: //
5:
6: #pragma once
7:
8: #include "object.h"
9: #include "ethernet.h"
10: #include <mutex>
11: #include <condition_variable>
12:
13: class NetDriver : public Object
14: {
15: public:
16: // コンストラクタ
17: NetDriver(const char *name, EthernetDevice *parent,
18: std::mutex *m, std::condition_variable *c);
19:
20: // デストラクタ。
21: virtual ~NetDriver();
22:
23: // 初期化 & オープン。
24: // EthernetDevice::Init() から呼ばれる。諸制約については device.h 参照。
25: // 必要なオープン処理や、必要なら受信スレッドの作成を行う。
26: // 成功すれば true、失敗すれば warn()/warnx() でメッセージを出力して
27: // false を返すこと。
28: // また、クローズ相当の動作はなくデストラクタで行う。
29: virtual bool Init() = 0;
30:
31: virtual bool MonitorUpdate();
32:
33: // パケットを送信する。正常に送信できれば true を返す。
34: // EthernetDevice::SendPacket() が呼ばれると常に呼び出される。
35: // その上で、ドライバが有効かどうか(オープンされているかどうかなど) は
36: // すべてこちら(NetDriver)側の責任。
37: // data はプリアンブルを含まず、イーサネットフレームヘッダ(14バイト)、
38: // データ(46-1500バイト) からなる。FCS(CRC) (4バイト) は含まない。
39: // VM から引き取ったパケットについて tx_pkts, tx_bytes を更新すること
40: // false が返った場合の規定が不明。
41: virtual bool SendPacket(const std::vector<uint8>& data) = 0;
42:
43: // 受信スレッド。
44: // VM に引き渡せたパケットについて rx_pkts, rx_bytes を更新すること。
45: virtual void ThreadRun() = 0;
46:
47: protected:
48: // オープンされていれば true を返す。
49: bool IsOpen() const { return fd != -1; }
50:
51: // 受信スレッドを起動する (必要なら継承クラス側が呼ぶ)
52: bool InitRecvThread();
53:
54: const char *drivername = NULL; // ドライバ名
55: EthernetDevice *parent = NULL; // 親
56:
57: std::string devpath; // デバイスファイルパス
58: std::string ifname; // 作成したインタフェース名
59:
60: // デバイスディスクリプタ。
61: // ディスクリプタを持つというのは共通なのでディスクリプタはここに置くが
62: // これを Open(), Close() するのはあくまで継承側の仕事で、こちらでは
63: // 初期化と IsOpen() だけ受け持つ。ちょっと責任分解点が気持ち悪いけど。
64: int fd = 0;
65:
66: // 親(Ethernet)デバイスの mutex と condvar
67: std::mutex *mtx;
68: std::condition_variable *cv;
69:
70: // 統計情報
71: uint64 tx_pkts = 0;
72: uint64 rx_pkts = 0;
73: uint64 tx_bytes = 0;
74: uint64 rx_bytes = 0;
75:
76: // ifup/ifdown スクリプト実行
77: bool Run_ifup();
78: bool Run_ifdown();
79:
80: private:
81: bool RunScript(const char *filename);
82: };
83:
1.1.1.2 ! root 84: extern std::unique_ptr<NetDriver> gNetDriver;
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.