1 /////////////////////////////////////////////////////////////////////////
2 // $Id: netmod.h 14182 2021-03-12 21:31:51Z vruppert $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 //  Copyright (C) 2001-2021  The Bochs Project
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Lesser General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 of the License, or (at your option) any later version.
11 //
12 //  This library is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //  Lesser General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Lesser General Public
18 //  License along with this library; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 //
21 
22 // Peter Grehan (grehan@iprg.nokia.com) coded the initial version of the
23 // NE2000/ether stuff.
24 
25 //  netmod.h  - see eth_null.cc for implementation details
26 
27 #ifndef BX_NETMOD_H
28 #define BX_NETMOD_H
29 
30 #define BX_PACKET_BUFSIZE 1514 // Maximum size of an ethernet frame
31 
32 // this should not be smaller than an arp reply with an ethernet header
33 #define MIN_RX_PACKET_LEN 60
34 
35 static const Bit8u broadcast_macaddr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
36 
get_net2(const Bit8u * buf)37 BX_CPP_INLINE Bit16u get_net2(const Bit8u *buf)
38 {
39   return (((Bit16u)*buf) << 8) |
40          ((Bit16u)*(buf+1));
41 }
42 
put_net2(Bit8u * buf,Bit16u data)43 BX_CPP_INLINE void put_net2(Bit8u *buf,Bit16u data)
44 {
45   *buf = (Bit8u)(data >> 8);
46   *(buf+1) = (Bit8u)(data & 0xff);
47 }
48 
get_net4(const Bit8u * buf)49 BX_CPP_INLINE Bit32u get_net4(const Bit8u *buf)
50 {
51   return (((Bit32u)*buf) << 24) |
52          (((Bit32u)*(buf+1)) << 16) |
53          (((Bit32u)*(buf+2)) << 8) |
54          ((Bit32u)*(buf+3));
55 }
56 
put_net4(Bit8u * buf,Bit32u data)57 BX_CPP_INLINE void put_net4(Bit8u *buf,Bit32u data)
58 {
59   *buf = (Bit8u)((data >> 24) & 0xff);
60   *(buf+1) = (Bit8u)((data >> 16) & 0xff);
61   *(buf+2) = (Bit8u)((data >> 8) & 0xff);
62   *(buf+3) = (Bit8u)(data & 0xff);
63 }
64 
65 #ifndef BXHUB
66 
67 // Pseudo device that loads the lowlevel networking module
68 class BOCHSAPI bx_netmod_ctl_c : public logfunctions {
69 public:
70   bx_netmod_ctl_c();
~bx_netmod_ctl_c()71   virtual ~bx_netmod_ctl_c() {}
72   void init(void);
73   const char **get_module_names();
74   void list_modules(void);
75   void exit(void);
76   virtual void* init_module(bx_list_c *base, void *rxh, void *rxstat, logfunctions *netdev);
77 };
78 
79 BOCHSAPI extern bx_netmod_ctl_c bx_netmod_ctl;
80 
81 // device receive status definitions
82 #define BX_NETDEV_RXREADY  0x0001
83 #define BX_NETDEV_SPEED    0x000e
84 #define BX_NETDEV_10MBIT   0x0002
85 #define BX_NETDEV_100MBIT  0x0004
86 #define BX_NETDEV_1GBIT    0x0008
87 
88 typedef void (*eth_rx_handler_t)(void *arg, const void *buf, unsigned len);
89 typedef Bit32u (*eth_rx_status_t)(void *arg);
90 
91 int execute_script(logfunctions *netdev, const char *name, char* arg1);
92 void BOCHSAPI_MSVCONLY write_pktlog_txt(FILE *pktlog_txt, const Bit8u *buf, unsigned len, bool host_to_guest);
93 size_t BOCHSAPI_MSVCONLY strip_whitespace(char *s);
94 
95 //
96 //  The eth_pktmover class is used by ethernet chip emulations
97 // to interface to the outside world. An instance of this
98 // would allow frames to be sent to and received from some
99 // entity. An example would be the packet filter on a Unix
100 // system, an NDIS driver in promisc mode on WinNT, or maybe
101 // a simulated network that talks to another process.
102 //
103 class eth_pktmover_c {
104 public:
105   virtual void sendpkt(void *buf, unsigned io_len) = 0;
~eth_pktmover_c()106   virtual ~eth_pktmover_c () {}
107 protected:
108   logfunctions *netdev;
109   eth_rx_handler_t  rxh;   // receive callback
110   eth_rx_status_t  rxstat; // receive status callback
111 };
112 
113 
114 //
115 //  The eth_locator class is used by pktmover classes to register
116 // their name. Chip emulations use the static 'create' method
117 // to locate and instantiate a pktmover class.
118 //
119 class BOCHSAPI_MSVCONLY eth_locator_c {
120 public:
121   static bool module_present(const char *type);
122   static void cleanup();
123   static eth_pktmover_c *create(const char *type, const char *netif,
124                                 const char *macaddr,
125                                 eth_rx_handler_t rxh,
126                                 eth_rx_status_t rxstat,
127                                 logfunctions *netdev,
128                                 const char *script);
129 protected:
130   eth_locator_c(const char *type);
131   virtual ~eth_locator_c();
132   virtual eth_pktmover_c *allocate(const char *netif,
133                                    const char *macaddr,
134                                    eth_rx_handler_t rxh,
135                                    eth_rx_status_t rxstat,
136                                    logfunctions *netdev,
137                                    const char *script) = 0;
138 private:
139   static eth_locator_c *all;
140   eth_locator_c *next;
141   const char *type;
142 };
143 
144 #endif
145 
146 #endif
147