From 1f750d92373f0013752b449d7d303f0a29f2606c Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Mon, 26 Aug 2024 07:05:39 -0600
Subject: [PATCH 1/6] Most modern network cards and routers uses standard
 frame.

Lowered the frame size because if network card / router can't handle high frame,
it will causes fragmentation, leading to packetloss or lost connection.
It depends on the network card and router.
A standard frame is 1518 bytes on the wire (as far as any capturing device is concerned).
A tagged frame (single tag) is 1522 bytes on the wire.
These take up 1538 bytes or 1542 bytes of transmission space on the wire.
On most OS, it is usually set at 1542. As a safe measure.
If one wants run on 90s network card, it should be set at 1518.
I think this should be automated, not hardcoded.
---
 src/Home/Net/Utilities/Net.HH | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/Home/Net/Utilities/Net.HH b/src/Home/Net/Utilities/Net.HH
index 0953fd61..34301a6e 100755
--- a/src/Home/Net/Utilities/Net.HH
+++ b/src/Home/Net/Utilities/Net.HH
@@ -8,9 +8,11 @@
 #define FCS_LENGTH					4
 
 /*	Ethernet Frame Size.
-	Linux uses 1544, OSDev and Shrine use 1548. Based on IEEE 802.3as, max frame size was agreed upon as 2000 bytes. */
-#define ETHERNET_FRAME_SIZE	2000
-
+	Based on IEEE 802.3 layer 1 Ethernet Max Frame is 1542 according to wiki. 72-1530 frame octet + 12 IPG octet
+	Default: 1538 | Vlan: 1542 | Jumbo: 9038 | JumboVlan: 9042 */
+#define ETHERNET_FRAME_SIZE     1542
+  
+// Max PayLoad standard: 1500 | Jumbo: 9000 for Gignet (fiber)
 #define ETHERNET_v2_MTU 1500
 
 #define HTYPE_ETHERNET	1

From 1a5368962baad9621effa3ad7f2137c80f51b270 Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Mon, 26 Aug 2024 07:16:48 -0600
Subject: [PATCH 2/6] There is no receive window size.

Client(send) overrides host(receive) window size.
It's one of the reasons why telnet chars and gopher files/links tend to drop. Packetloss/timeout.
---
 src/Home/Net/Protocols/TCP/TCP.HH |  7 +++++--
 src/Home/Net/Protocols/TCP/TCP.ZC | 10 +++++-----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/Home/Net/Protocols/TCP/TCP.HH b/src/Home/Net/Protocols/TCP/TCP.HH
index d4ab6a8d..e24c7f36 100755
--- a/src/Home/Net/Protocols/TCP/TCP.HH
+++ b/src/Home/Net/Protocols/TCP/TCP.HH
@@ -6,7 +6,10 @@
 #define TCP_RTO_MIN		0.2
 #define TCP_RTO_MAX		10
 
-#define TCP_WINDOW_SIZE	8192
+#define TCP_WINDOW_SIZE	8192 // Should not be static. For now it's okay
+
+U16 RECEIVE_WINDOW; // gets host window size. It is not static.
+U16 SEND_WINDOW; // gets receive window size from host, then adjust accordingly. 
 
 #define TCP_MSS			536 // Max Segment Size default
 
@@ -24,7 +27,7 @@
 #define TCP_STATE_LAST_ACK		9
 #define TCP_STATE_TIME_WAIT		10
 
-// TCP header flags. Test with Bt(), e.g. Bt(&flags, TCPf_RST)
+// TCP header flags. Order of Operation matters. Test with Bt(), e.g. Bt(&flags, TCPf_RST)
 #define TCPf_FIN	0
 #define TCPf_SYN	1
 #define TCPf_RST	2
diff --git a/src/Home/Net/Protocols/TCP/TCP.ZC b/src/Home/Net/Protocols/TCP/TCP.ZC
index 770256c2..77bc836b 100755
--- a/src/Home/Net/Protocols/TCP/TCP.ZC
+++ b/src/Home/Net/Protocols/TCP/TCP.ZC
@@ -101,7 +101,7 @@ I64 TCPPacketAllocate(U8 **frame_out,
 	header->ack_num				= EndianU32(ack_num);
 	header->data_offset			= (sizeof(CTCPHeader) / 4) << 4; // ???
 	header->flags				= flags;
-	header->window_size			= EndianU16(TCP_WINDOW_SIZE / 2);// TODO: What is window size supposed to be ?
+	header->window_size			= EndianU16(TCP_WINDOW_SIZE);// Window Size allocation data. Adjusted based on receive window. 
 	header->checksum			= 0;
 	header->urgent_pointer		= 0;
 
@@ -647,8 +647,8 @@ CTCPSocket TCPSocket(U16 domain=AF_UNSPEC)
 	QueueInit(accept_queue); // init pending connection queue
 	tcp_socket->accept_queue = accept_queue;
 
-	tcp_socket->receive_buffer_size = TCP_WINDOW_SIZE;
-	tcp_socket->receive_buffer = CAlloc(TCP_WINDOW_SIZE);
+	tcp_socket->receive_buffer_size = RECEIVE_WINDOW;
+	tcp_socket->receive_buffer = CAlloc(RECEIVE_WINDOW);
 
 	tcp_socket->max_segment_size = TCP_MSS;
 
@@ -1057,7 +1057,7 @@ I64 TCPSocketConnect(CTCPSocket *tcp_socket, CSocketAddressStorage *address)
 	}
 
 	tcp_socket->connection_time = tS;
-	tcp_socket->receive_window	= TCP_WINDOW_SIZE;
+	tcp_socket->receive_window	= RECEIVE_WINDOW;
 
 	tcp_socket->state = TCP_STATE_SYN_SENT;
 	TCPSendFlags(tcp_socket, TCPF_SYN);
@@ -1165,7 +1165,7 @@ CTCPSocket *TCPSocketAccept(CTCPSocket *tcp_socket)
 
 	new_socket->next_recv_seq_num	= ++pending->segment_seq_num;
 	new_socket->connection_time		= tS;
-	new_socket->receive_window		= TCP_WINDOW_SIZE;
+	new_socket->receive_window		= RECEIVE_WINDOW;
 	new_socket->timeout				= tcp_socket->timeout;
 
 	temp_addr = &tcp_socket->source_address;

From ff978b1ce3486088c19c31b63a4f7fc55cdf48ae Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Mon, 26 Aug 2024 18:34:11 -0600
Subject: [PATCH 3/6] RECEIVE WINDOW is different than RECEIVE BUFFER

The receive buffer is allocated per socket.
TCP receive buffer holds TCP data that has not yet been processed
(consumed via read/recv system calls) by the application.
---
 src/Home/Net/Protocols/TCP/TCP.HH | 2 ++
 src/Home/Net/Protocols/TCP/TCP.ZC | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/Home/Net/Protocols/TCP/TCP.HH b/src/Home/Net/Protocols/TCP/TCP.HH
index e24c7f36..f76ae553 100755
--- a/src/Home/Net/Protocols/TCP/TCP.HH
+++ b/src/Home/Net/Protocols/TCP/TCP.HH
@@ -11,6 +11,8 @@
 U16 RECEIVE_WINDOW; // gets host window size. It is not static.
 U16 SEND_WINDOW; // gets receive window size from host, then adjust accordingly. 
 
+U32 RECEIVE_BUFFER; // is 32 too much? receive_buffer holds TCP data that has not yet been processed
+
 #define TCP_MSS			536 // Max Segment Size default
 
 #define TCP_TIMEOUT		5000
diff --git a/src/Home/Net/Protocols/TCP/TCP.ZC b/src/Home/Net/Protocols/TCP/TCP.ZC
index 77bc836b..22e4edcc 100755
--- a/src/Home/Net/Protocols/TCP/TCP.ZC
+++ b/src/Home/Net/Protocols/TCP/TCP.ZC
@@ -647,8 +647,8 @@ CTCPSocket TCPSocket(U16 domain=AF_UNSPEC)
 	QueueInit(accept_queue); // init pending connection queue
 	tcp_socket->accept_queue = accept_queue;
 
-	tcp_socket->receive_buffer_size = RECEIVE_WINDOW;
-	tcp_socket->receive_buffer = CAlloc(RECEIVE_WINDOW);
+	tcp_socket->receive_buffer_size = RECEIVE_BUFFER;
+	tcp_socket->receive_buffer = CAlloc(RECEIVE_BUFFER);
 
 	tcp_socket->max_segment_size = TCP_MSS;
 

From e363a0b0b67b462fede592950c83abcb4790a132 Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Tue, 27 Aug 2024 13:50:40 -0600
Subject: [PATCH 4/6] Unsure if this is correct but as far as connections and
 speeds goes, it remained unchanged.

---
 src/Home/Net/Load.ZC              | 5 +++--
 src/Home/Net/Protocols/TCP/TCP.HH | 4 ++--
 src/Home/Net/Protocols/TCP/TCP.ZC | 2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/Home/Net/Load.ZC b/src/Home/Net/Load.ZC
index 1dc0981e..6ce0057f 100755
--- a/src/Home/Net/Load.ZC
+++ b/src/Home/Net/Load.ZC
@@ -20,8 +20,9 @@ Cd(__DIR__);;
 #include "Protocols/DNS"
 #include "Utilities/Ping"
 
-#include "Protocols/TCP/MakeTCP"
+//#include "Protocols/TCP/MakeTCP"
+#include "Protocols/TCP/TCP_V2.ZC
 
 #include "Protocols/DHCP"
 
-#include "Utilities/NetHandler" // needs IPV4, UDP, ICMP
\ No newline at end of file
+#include "Utilities/NetHandler" // needs IPV4, UDP, ICMP
diff --git a/src/Home/Net/Protocols/TCP/TCP.HH b/src/Home/Net/Protocols/TCP/TCP.HH
index f76ae553..e0a9d6dc 100755
--- a/src/Home/Net/Protocols/TCP/TCP.HH
+++ b/src/Home/Net/Protocols/TCP/TCP.HH
@@ -6,8 +6,8 @@
 #define TCP_RTO_MIN		0.2
 #define TCP_RTO_MAX		10
 
-#define TCP_WINDOW_SIZE	8192 // Should not be static. For now it's okay
-
+// #define TCP_WINDOW_SIZE	8192 // Should not be static. For now it's okay
+U16 WINDOW_SIZE; // Init window size in CTPCPacketAllocate{} ?? Need more study
 U16 RECEIVE_WINDOW; // gets host window size. It is not static.
 U16 SEND_WINDOW; // gets receive window size from host, then adjust accordingly. 
 
diff --git a/src/Home/Net/Protocols/TCP/TCP.ZC b/src/Home/Net/Protocols/TCP/TCP.ZC
index 22e4edcc..619e5886 100755
--- a/src/Home/Net/Protocols/TCP/TCP.ZC
+++ b/src/Home/Net/Protocols/TCP/TCP.ZC
@@ -101,7 +101,7 @@ I64 TCPPacketAllocate(U8 **frame_out,
 	header->ack_num				= EndianU32(ack_num);
 	header->data_offset			= (sizeof(CTCPHeader) / 4) << 4; // ???
 	header->flags				= flags;
-	header->window_size			= EndianU16(TCP_WINDOW_SIZE);// Window Size allocation data. Adjusted based on receive window. 
+	header->window_size			= EndianU16(WINDOW_SIZE);// Window Size allocation data. Adjusted based on receive window. 
 	header->checksum			= 0;
 	header->urgent_pointer		= 0;
 

From 295a97bf4cf1e31938972395a641d152c1832437 Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Tue, 27 Aug 2024 19:04:03 -0600
Subject: [PATCH 5/6] changed hardcoded to systemside clock or ticks.
 JIFFY_FREQ.

Noticed slightly better responsive times while reducing timeouts.
---
 src/Home/Net/Protocols/TCP/TCP.HH | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/Home/Net/Protocols/TCP/TCP.HH b/src/Home/Net/Protocols/TCP/TCP.HH
index e0a9d6dc..3b7ce781 100755
--- a/src/Home/Net/Protocols/TCP/TCP.HH
+++ b/src/Home/Net/Protocols/TCP/TCP.HH
@@ -2,20 +2,28 @@
 
 #define TCP_SRTT_ALPHA	0.9
 
+// Transmission Time Out to prevent network slugglish performance
 #define TCP_RTO_BETA	2
-#define TCP_RTO_MIN		0.2
-#define TCP_RTO_MAX		10
+#define TCP_RTO_MIN		JIFFY_FREQ/5 // (linux TCP.H) should be using system timer/clock instead hardcoded
+#define TCP_RTO_MAX		120*JIFFY_FREQ // JIFFY_FREZ = Hz. Hz is software clock ticks
 
-// #define TCP_WINDOW_SIZE	8192 // Should not be static. For now it's okay
-U16 WINDOW_SIZE; // Init window size in CTPCPacketAllocate{} ?? Need more study
-U16 RECEIVE_WINDOW; // gets host window size. It is not static.
-U16 SEND_WINDOW; // gets receive window size from host, then adjust accordingly. 
+#define TCP_MAX_WINDOW	32676	// Beyond this number, use window_scale(not implemented). 8192 is recommended/default.
+U16 WINDOW_SIZE; 		// Initiate window size in CTPCPacketAllocate{} ?? Need more study
+U16 RECEIVE_WINDOW; 		// gets host window size. It is not static.
+U16 SEND_WINDOW; 		// gets receive window size from host, then adjust accordingly.
 
-U32 RECEIVE_BUFFER; // is 32 too much? receive_buffer holds TCP data that has not yet been processed
+U32 RECEIVE_BUFFER; 		// is 32 too much? receive_buffer holds TCP data that has not yet been processed
 
 #define TCP_MSS			536 // Max Segment Size default
+#define TCP_MIN_MSS		88  // Min Segment Size
 
-#define TCP_TIMEOUT		5000
+#define TCP_INIT_TIMEOUT	1*JIFFY_FREQ  // TODO... Init RTO Value. Not sure if this is needed.
+#define TCP_TIMEOUT		20*JIFFY_FREQ // Roughly a minute based on software clock
+#define TCP_KEEPALIVE		120*JIFFY_FREQ // TODO... 1 hour
+
+#define TCP_MAX_INCREASEACK	16 //TODO... At Initial start up, it's generally slow. This accelerates the process.
+#define TCP_MAX_DELAYACK	JIFFY_FREQ/5 //TODO... Delay max time ack
+#define TCP_MIN_DELAYACK	JIFFY_FREQ/25 //TODO... Delay min time ack
 
 #define TCP_STATE_CLOSED		0
 #define TCP_STATE_LISTEN		1
@@ -44,6 +52,8 @@ U32 RECEIVE_BUFFER; // is 32 too much? receive_buffer holds TCP data that has no
 #define TCPF_ACK	(1 << TCPf_ACK)
 //#define TCPF_URG	(1 << TCPf_URG) // most stacks don't implement URGENT.
 
+// U32 TCP_MAX_DELAYACK(CSocket *Socket); // TODO 
+
 
 class CTCPAckQueue:CQueue
 {

From dbe88c18c9e6bd929cbd954a65b2e46c2b78f32f Mon Sep 17 00:00:00 2001
From: Z8Griz <154282263+Z8Griz@users.noreply.github.com>
Date: Tue, 27 Aug 2024 20:21:38 -0600
Subject: [PATCH 6/6] did not mean to include that specific code and file.

---
 src/Home/Net/Load.ZC | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/Home/Net/Load.ZC b/src/Home/Net/Load.ZC
index 6ce0057f..667ef666 100755
--- a/src/Home/Net/Load.ZC
+++ b/src/Home/Net/Load.ZC
@@ -20,8 +20,7 @@ Cd(__DIR__);;
 #include "Protocols/DNS"
 #include "Utilities/Ping"
 
-//#include "Protocols/TCP/MakeTCP"
-#include "Protocols/TCP/TCP_V2.ZC
+#include "Protocols/TCP/MakeTCP"
 
 #include "Protocols/DHCP"