Browse Source

[tcpip] Provide tcpip_mtu() to determine the maximum transmission unit

Provide the function tcpip_mtu() to allow external code to determine
the (transport-layer) maximum transmission unit for a given socket
address.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
6414b5ca03
4 changed files with 34 additions and 0 deletions
  1. 3
    0
      src/include/ipxe/tcpip.h
  2. 1
    0
      src/net/ipv4.c
  3. 1
    0
      src/net/ipv6.c
  4. 29
    0
      src/net/tcpip.c

+ 3
- 0
src/include/ipxe/tcpip.h View File

97
 	const char *name;
97
 	const char *name;
98
 	/** Network address family */
98
 	/** Network address family */
99
 	sa_family_t sa_family;
99
 	sa_family_t sa_family;
100
+	/** Fixed header length */
101
+	size_t header_len;
100
 	/**
102
 	/**
101
 	 * Transmit packet
103
 	 * Transmit packet
102
 	 *
104
 	 *
148
 		      struct net_device *netdev,
150
 		      struct net_device *netdev,
149
 		      uint16_t *trans_csum );
151
 		      uint16_t *trans_csum );
150
 extern struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest );
152
 extern struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest );
153
+extern size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest );
151
 extern uint16_t generic_tcpip_continue_chksum ( uint16_t partial,
154
 extern uint16_t generic_tcpip_continue_chksum ( uint16_t partial,
152
 						const void *data, size_t len );
155
 						const void *data, size_t len );
153
 extern uint16_t tcpip_chksum ( const void *data, size_t len );
156
 extern uint16_t tcpip_chksum ( const void *data, size_t len );

+ 1
- 0
src/net/ipv4.c View File

621
 struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
621
 struct tcpip_net_protocol ipv4_tcpip_protocol __tcpip_net_protocol = {
622
 	.name = "IPv4",
622
 	.name = "IPv4",
623
 	.sa_family = AF_INET,
623
 	.sa_family = AF_INET,
624
+	.header_len = sizeof ( struct iphdr ),
624
 	.tx = ipv4_tx,
625
 	.tx = ipv4_tx,
625
 	.netdev = ipv4_netdev,
626
 	.netdev = ipv4_netdev,
626
 };
627
 };

+ 1
- 0
src/net/ipv6.c View File

988
 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
988
 struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
989
 	.name = "IPv6",
989
 	.name = "IPv6",
990
 	.sa_family = AF_INET6,
990
 	.sa_family = AF_INET6,
991
+	.header_len = sizeof ( struct ipv6_header ),
991
 	.tx = ipv6_tx,
992
 	.tx = ipv6_tx,
992
 	.netdev = ipv6_netdev,
993
 	.netdev = ipv6_netdev,
993
 };
994
 };

+ 29
- 0
src/net/tcpip.c View File

6
 #include <ipxe/iobuf.h>
6
 #include <ipxe/iobuf.h>
7
 #include <ipxe/tables.h>
7
 #include <ipxe/tables.h>
8
 #include <ipxe/ipstat.h>
8
 #include <ipxe/ipstat.h>
9
+#include <ipxe/netdevice.h>
9
 #include <ipxe/tcpip.h>
10
 #include <ipxe/tcpip.h>
10
 
11
 
11
 /** @file
12
 /** @file
122
 	return NULL;
123
 	return NULL;
123
 }
124
 }
124
 
125
 
126
+/**
127
+ * Determine maximum transmission unit
128
+ *
129
+ * @v st_dest		Destination address
130
+ * @ret mtu		Maximum transmission unit
131
+ */
132
+size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest ) {
133
+	struct tcpip_net_protocol *tcpip_net;
134
+	struct net_device *netdev;
135
+	size_t mtu;
136
+
137
+	/* Find appropriate network-layer protocol */
138
+	tcpip_net = tcpip_net_protocol ( st_dest );
139
+	if ( ! tcpip_net )
140
+		return 0;
141
+
142
+	/* Find transmitting network device */
143
+	netdev = tcpip_net->netdev ( st_dest );
144
+	if ( ! netdev )
145
+		return 0;
146
+
147
+	/* Calculate MTU */
148
+	mtu = ( netdev->max_pkt_len - netdev->ll_protocol->ll_header_len -
149
+		tcpip_net->header_len );
150
+
151
+	return mtu;
152
+}
153
+
125
 /**
154
 /**
126
  * Calculate continued TCP/IP checkum
155
  * Calculate continued TCP/IP checkum
127
  *
156
  *

Loading…
Cancel
Save