Annotation of nono/host/netdriver.h, revision 1.1.1.6

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.