Browse Source

Allow udp_{send,sendto,sendto_via} to be called outside of the

udp_senddata() callback.  This will simplify any UDP protocols that don't
need the temporary buffer.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
753e156844
1 changed files with 39 additions and 32 deletions
  1. 39
    32
      src/net/udp.c

+ 39
- 32
src/net/udp.c View File

85
 	DBGC ( conn, "UDP %p closed\n", conn );
85
 	DBGC ( conn, "UDP %p closed\n", conn );
86
 }
86
 }
87
 
87
 
88
+/**
89
+ * Allocate packet buffer for UDP
90
+ *
91
+ * @v conn		UDP connection
92
+ * @ret pkb		Packet buffer, or NULL
93
+ */
94
+static struct pk_buff * udp_alloc_pkb ( struct udp_connection *conn ) {
95
+	struct pk_buff *pkb;
96
+
97
+	pkb = alloc_pkb ( UDP_MAX_TXPKB );
98
+	if ( ! pkb ) {
99
+		DBGC ( conn, "UDP %p cannot allocate buffer of length %d\n",
100
+		       conn, UDP_MAX_TXPKB );
101
+		return NULL;
102
+	}
103
+	pkb_reserve ( pkb, UDP_MAX_HLEN );
104
+	return pkb;
105
+}
106
+
88
 /**
107
 /**
89
  * User request to send data via a UDP connection
108
  * User request to send data via a UDP connection
90
  *
109
  *
91
  * @v conn		UDP connection
110
  * @v conn		UDP connection
92
  *
111
  *
93
- * This function allocates buffer space and invokes the function's senddata()
94
- * callback. The callback may use the buffer space
112
+ * This function allocates buffer space and invokes the function's
113
+ * senddata() callback.  The callback may use the buffer space as
114
+ * temporary storage space.
95
  */
115
  */
96
 int udp_senddata ( struct udp_connection *conn ) {
116
 int udp_senddata ( struct udp_connection *conn ) {
97
 	int rc;
117
 	int rc;
98
 
118
 
99
-	conn->tx_pkb = alloc_pkb ( UDP_MAX_TXPKB );
100
-	if ( conn->tx_pkb == NULL ) {
101
-		DBGC ( conn, "UDP %p cannot allocate buffer of length %d\n",
102
-		       conn, UDP_MAX_TXPKB );
119
+	conn->tx_pkb = udp_alloc_pkb ( conn );
120
+	if ( ! conn->tx_pkb )
103
 		return -ENOMEM;
121
 		return -ENOMEM;
104
-	}
105
-	pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
122
+
106
 	rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data, 
123
 	rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data, 
107
 				      pkb_tailroom ( conn->tx_pkb ) );
124
 				      pkb_tailroom ( conn->tx_pkb ) );
108
-	if ( conn->tx_pkb )
125
+
126
+	if ( conn->tx_pkb ) {
109
 		free_pkb ( conn->tx_pkb );
127
 		free_pkb ( conn->tx_pkb );
128
+		conn->tx_pkb = NULL;
129
+	}
130
+
110
 	return rc;
131
 	return rc;
111
 }
132
 }
112
 		
133
 		
119
  * @v data		Data to send
140
  * @v data		Data to send
120
  * @v len		Length of data
141
  * @v len		Length of data
121
  * @ret rc		Return status code
142
  * @ret rc		Return status code
122
- *
123
- * This function fills up the UDP headers and sends the data.  It may
124
- * be called only from within the context of an application's
125
- * senddata() method; if the application wishes to send data it must
126
- * call udp_senddata() and wait for its senddata() method to be
127
- * called.
128
  */
143
  */
129
 int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
144
 int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
130
 		     struct net_device *netdev, const void *data,
145
 		     struct net_device *netdev, const void *data,
132
        	struct udp_header *udphdr;
147
        	struct udp_header *udphdr;
133
 	struct pk_buff *pkb;
148
 	struct pk_buff *pkb;
134
 
149
 
135
-	/* Take ownership of packet buffer back from the
136
-	 * udp_connection structure.
137
-	 */
138
-	pkb = conn->tx_pkb;
139
-	conn->tx_pkb = NULL;
150
+	/* Use precreated packet buffer if one is available */
151
+	if ( conn->tx_pkb ) {
152
+		pkb = conn->tx_pkb;
153
+		conn->tx_pkb = NULL;
154
+	} else {
155
+		pkb = udp_alloc_pkb ( conn );
156
+		if ( ! pkb )
157
+			return -ENOMEM;
158
+	}
140
 
159
 
141
 	/* Avoid overflowing TX buffer */
160
 	/* Avoid overflowing TX buffer */
142
 	if ( len > pkb_tailroom ( pkb ) )
161
 	if ( len > pkb_tailroom ( pkb ) )
175
  * @v data		Data to send
194
  * @v data		Data to send
176
  * @v len		Length of data
195
  * @v len		Length of data
177
  * @ret rc		Return status code
196
  * @ret rc		Return status code
178
- *
179
- * This function fills up the UDP headers and sends the data.  It may
180
- * be called only from within the context of an application's
181
- * senddata() method; if the application wishes to send data it must
182
- * call udp_senddata() and wait for its senddata() method to be
183
- * called.
184
  */
197
  */
185
 int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
198
 int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
186
 		 const void *data, size_t len ) {
199
 		 const void *data, size_t len ) {
194
  * @v data		Data to send
207
  * @v data		Data to send
195
  * @v len		Length of data
208
  * @v len		Length of data
196
  * @ret rc		Return status code
209
  * @ret rc		Return status code
197
- *
198
- * This function fills up the UDP headers and sends the data.  It may
199
- * be called only from within the context of an application's
200
- * senddata() method; if the application wishes to send data it must
201
- * call udp_senddata() and wait for its senddata() method to be
202
- * called.
203
  */
210
  */
204
 int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
211
 int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
205
 	return udp_sendto ( conn, &conn->peer, data, len );
212
 	return udp_sendto ( conn, &conn->peer, data, len );

Loading…
Cancel
Save