Kaynağa Gözat

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 yıl önce
ebeveyn
işleme
753e156844
1 değiştirilmiş dosya ile 39 ekleme ve 32 silme
  1. 39
    32
      src/net/udp.c

+ 39
- 32
src/net/udp.c Dosyayı Görüntüle

@@ -85,28 +85,49 @@ void udp_close ( struct udp_connection *conn ) {
85 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 108
  * User request to send data via a UDP connection
90 109
  *
91 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 116
 int udp_senddata ( struct udp_connection *conn ) {
97 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 121
 		return -ENOMEM;
104
-	}
105
-	pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
122
+
106 123
 	rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data, 
107 124
 				      pkb_tailroom ( conn->tx_pkb ) );
108
-	if ( conn->tx_pkb )
125
+
126
+	if ( conn->tx_pkb ) {
109 127
 		free_pkb ( conn->tx_pkb );
128
+		conn->tx_pkb = NULL;
129
+	}
130
+
110 131
 	return rc;
111 132
 }
112 133
 		
@@ -119,12 +140,6 @@ int udp_senddata ( struct udp_connection *conn ) {
119 140
  * @v data		Data to send
120 141
  * @v len		Length of data
121 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 144
 int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
130 145
 		     struct net_device *netdev, const void *data,
@@ -132,11 +147,15 @@ int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
132 147
        	struct udp_header *udphdr;
133 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 160
 	/* Avoid overflowing TX buffer */
142 161
 	if ( len > pkb_tailroom ( pkb ) )
@@ -175,12 +194,6 @@ int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
175 194
  * @v data		Data to send
176 195
  * @v len		Length of data
177 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 198
 int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
186 199
 		 const void *data, size_t len ) {
@@ -194,12 +207,6 @@ int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
194 207
  * @v data		Data to send
195 208
  * @v len		Length of data
196 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 211
 int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
205 212
 	return udp_sendto ( conn, &conn->peer, data, len );

Loading…
İptal
Kaydet