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