Browse Source

Actually, it's probably a good idea to have packet buffers avoid 4kB

crossings.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
cf3783b4ca
2 changed files with 30 additions and 7 deletions
  1. 11
    0
      src/include/gpxe/pkbuff.h
  2. 19
    7
      src/net/pkbuff.c

+ 11
- 0
src/include/gpxe/pkbuff.h View File

@@ -17,6 +17,17 @@
17 17
 struct net_protocol;
18 18
 struct ll_protocol;
19 19
 
20
+/**
21
+ * Packet buffer alignment
22
+ *
23
+ * Packet buffers allocated via alloc_pkb() are guaranteed to be
24
+ * physically aligned to this boundary.  Some cards cannot DMA across
25
+ * a 4kB boundary.  With a standard Ethernet MTU, aligning to a 2kB
26
+ * boundary is sufficient to guarantee no 4kB boundary crossings.  For
27
+ * a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
28
+ */
29
+#define PKBUFF_ALIGN 2048
30
+
20 31
 /** A packet buffer
21 32
  *
22 33
  * This structure is used to represent a network packet within gPXE.

+ 19
- 7
src/net/pkbuff.c View File

@@ -31,24 +31,36 @@
31 31
  *
32 32
  * @v len	Required length of buffer
33 33
  * @ret pkb	Packet buffer, or NULL if none available
34
+ *
35
+ * The packet buffer will be physically aligned to a multiple of
36
+ * @c PKBUFF_SIZE.
34 37
  */
35 38
 struct pk_buff * alloc_pkb ( size_t len ) {
36 39
 	struct pk_buff *pkb = NULL;
40
+	void *data;
37 41
 
42
+	/* Align buffer length */
43
+	len = ( len + __alignof__ ( *pkb ) - 1 ) & ~ __alignof__ ( *pkb );
44
+	
38 45
 	/* Allocate memory for buffer plus descriptor */
39
-	pkb = malloc ( sizeof ( *pkb ) + len );
40
-	if ( pkb ) {
41
-		pkb->head = pkb->data = pkb->tail = ( void * ) ( pkb + 1 );
42
-		pkb->end = pkb->head + len;
43
-	}
46
+	data = malloc_dma ( len + sizeof ( *pkb ), PKBUFF_ALIGN );
47
+	if ( ! data )
48
+		return NULL;
49
+
50
+	pkb = ( struct pk_buff * ) ( data + len );
51
+	pkb->head = pkb->data = pkb->tail = data;
52
+	pkb->end = pkb;
44 53
 	return pkb;
45 54
 }
46 55
 
47 56
 /**
48 57
  * Free packet buffer
49 58
  *
50
- * @v pkb	Packet buffer, or NULL
59
+ * @v pkb	Packet buffer
51 60
  */
52 61
 void free_pkb ( struct pk_buff *pkb ) {
53
-	free ( pkb );
62
+	if ( pkb ) {
63
+		free_dma ( pkb->head,
64
+			   ( pkb->end - pkb->head ) + sizeof ( *pkb ) );
65
+	}
54 66
 }

Loading…
Cancel
Save