瀏覽代碼

[DHCP] Save precious packet-aligned memory by copying DHCP responses

Copy DHCP responses to a standard malloc()ed buffer, rather than
retaining the I/O buffer that they arrived in.
tags/v0.9.4
Michael Brown 16 年之前
父節點
當前提交
83617e5b1c
共有 1 個文件被更改,包括 16 次插入33 次删除
  1. 16
    33
      src/net/udp/dhcp.c

+ 16
- 33
src/net/udp/dhcp.c 查看文件

342
 struct dhcp_settings {
342
 struct dhcp_settings {
343
 	/** Reference counter */
343
 	/** Reference counter */
344
 	struct refcnt refcnt;
344
 	struct refcnt refcnt;
345
-	/** Containing I/O buffer */
346
-	struct io_buffer *iobuf;
347
 	/** DHCP packet */
345
 	/** DHCP packet */
348
 	struct dhcp_packet dhcppkt;
346
 	struct dhcp_packet dhcppkt;
349
 	/** Setting interface */
347
 	/** Setting interface */
350
 	struct settings settings;
348
 	struct settings settings;
351
 };
349
 };
352
 
350
 
353
-/**
354
- * Free DHCP settings block
355
- *
356
- * @v refcnt		Reference counter
357
- */
358
-static void dhcpset_free ( struct refcnt *refcnt ) {
359
-	struct dhcp_settings *dhcpset =
360
-		container_of ( refcnt, struct dhcp_settings, refcnt );
361
-
362
-	free_iob ( dhcpset->iobuf );
363
-	free ( dhcpset );
364
-}
365
-
366
 /**
351
 /**
367
  * Decrement reference count on DHCP settings block
352
  * Decrement reference count on DHCP settings block
368
  *
353
  *
413
 };
398
 };
414
 
399
 
415
 /**
400
 /**
416
- * Create DHCP setting block from I/O buffer
401
+ * Create DHCP setting block
417
  *
402
  *
418
- * @v iobuf		I/O buffer
403
+ * @v dhcphdr		DHCP packet
404
+ * @v len		Length of DHCP packet
419
  * @ret dhcpset		DHCP settings block
405
  * @ret dhcpset		DHCP settings block
420
- *
421
- * This function takes ownership of the I/O buffer.  Future accesses
422
- * must be via the @c dhcpset data structure.
423
  */
406
  */
424
-static struct dhcp_settings * dhcpset_create_iob ( struct io_buffer *iobuf ) {
407
+static struct dhcp_settings * dhcpset_create ( const struct dhcphdr *dhcphdr,
408
+					       size_t len ) {
425
 	struct dhcp_settings *dhcpset;
409
 	struct dhcp_settings *dhcpset;
410
+	void *data;
426
 
411
 
427
-	dhcpset = zalloc ( sizeof ( *dhcpset ) );
412
+	dhcpset = zalloc ( sizeof ( *dhcpset ) + len );
428
 	if ( dhcpset ) {
413
 	if ( dhcpset ) {
429
-		dhcpset->refcnt.free = dhcpset_free;
430
-		dhcpset->iobuf = iobuf;
431
-		dhcppkt_init ( &dhcpset->dhcppkt,
432
-			       iobuf->data, iob_len ( iobuf ) );
414
+		data = ( ( ( void * ) dhcpset ) + sizeof ( *dhcpset ) );
415
+		memcpy ( data, dhcphdr, len );
416
+		dhcppkt_init ( &dhcpset->dhcppkt, data, len );
433
 		settings_init ( &dhcpset->settings,
417
 		settings_init ( &dhcpset->settings,
434
 				&dhcpset_settings_operations, &dhcpset->refcnt,
418
 				&dhcpset_settings_operations, &dhcpset->refcnt,
435
 				DHCP_SETTINGS_NAME );
419
 				DHCP_SETTINGS_NAME );
632
  * @v len		Length of received data
616
  * @v len		Length of received data
633
  * @ret rc		Return status code
617
  * @ret rc		Return status code
634
  */
618
  */
635
-static int dhcp_deliver_iob ( struct xfer_interface *xfer,
636
-			      struct io_buffer *iobuf,
637
-			      struct xfer_metadata *meta __unused ) {
619
+static int dhcp_deliver_raw ( struct xfer_interface *xfer,
620
+			      const void *data, size_t len ) {
638
 	struct dhcp_session *dhcp =
621
 	struct dhcp_session *dhcp =
639
 		container_of ( xfer, struct dhcp_session, xfer );
622
 		container_of ( xfer, struct dhcp_session, xfer );
640
 	struct dhcp_settings *response;
623
 	struct dhcp_settings *response;
648
 	uint8_t ignore_proxy = 0;
631
 	uint8_t ignore_proxy = 0;
649
 	int rc;
632
 	int rc;
650
 
633
 
651
-	/* Convert packet into a DHCP-packet-in-iobuf */
652
-	response = dhcpset_create_iob ( iobuf );
634
+	/* Convert packet into a DHCP settings block */
635
+	response = dhcpset_create ( data, len );
653
 	if ( ! response ) {
636
 	if ( ! response ) {
654
 		DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
637
 		DBGC ( dhcp, "DHCP %p could not store DHCP packet\n", dhcp );
655
 		return -ENOMEM;
638
 		return -ENOMEM;
748
 	.vredirect	= xfer_vopen,
731
 	.vredirect	= xfer_vopen,
749
 	.window		= unlimited_xfer_window,
732
 	.window		= unlimited_xfer_window,
750
 	.alloc_iob	= default_xfer_alloc_iob,
733
 	.alloc_iob	= default_xfer_alloc_iob,
751
-	.deliver_iob	= dhcp_deliver_iob,
752
-	.deliver_raw	= xfer_deliver_as_iob,
734
+	.deliver_iob	= xfer_deliver_as_raw,
735
+	.deliver_raw	= dhcp_deliver_raw,
753
 };
736
 };
754
 
737
 
755
 /****************************************************************************
738
 /****************************************************************************

Loading…
取消
儲存