github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/tcpip/link/sharedmem/pipe/tx.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pipe
    16  
    17  // Tx is the transmit side of the shared memory ring buffer.
    18  type Tx struct {
    19  	p              pipe
    20  	maxPayloadSize uint64
    21  
    22  	head uint64
    23  	tail uint64
    24  	next uint64
    25  
    26  	tailHeader uint64
    27  }
    28  
    29  // Init initializes the transmit end of the pipe. In the initial state, the next
    30  // slot to be written is the very first one, and the transmitter has the whole
    31  // ring buffer available to it.
    32  func (t *Tx) Init(b []byte) {
    33  	t.p.init(b)
    34  	// maxPayloadSize excludes the header of the payload, and the header
    35  	// of the wrapping message.
    36  	t.maxPayloadSize = uint64(len(t.p.buffer)) - 2*sizeOfSlotHeader
    37  	t.tail = 0xfffffffe * jump
    38  	t.next = t.tail
    39  	t.head = t.tail + jump
    40  	t.p.write(t.tail, slotFree)
    41  }
    42  
    43  // Capacity determines how many records of the given size can be written to the
    44  // pipe before it fills up.
    45  func (t *Tx) Capacity(recordSize uint64) uint64 {
    46  	available := uint64(len(t.p.buffer)) - sizeOfSlotHeader
    47  	entryLen := payloadToSlotSize(recordSize)
    48  	return available / entryLen
    49  }
    50  
    51  // Push reserves "payloadSize" bytes for transmission in the pipe. The caller
    52  // populates the returned slice with the data to be transferred and enventually
    53  // calls Flush() to make the data visible to the reader, or Abort() to make the
    54  // pipe forget all Push() calls since the last Flush().
    55  //
    56  // The returned slice is available until Flush() or Abort() is next called.
    57  // After that, it must not be touched.
    58  func (t *Tx) Push(payloadSize uint64) []byte {
    59  	// Fail request if we know we will never have enough room.
    60  	if payloadSize > t.maxPayloadSize {
    61  		return nil
    62  	}
    63  
    64  	// True if TxPipe currently has a pushed message, i.e., it is not
    65  	// Flush()'ed.
    66  	messageAhead := t.next != t.tail
    67  	totalLen := payloadToSlotSize(payloadSize)
    68  	newNext := t.next + totalLen
    69  	nextWrap := (t.next & revolutionMask) | uint64(len(t.p.buffer))
    70  	if int64(newNext-nextWrap) >= 0 {
    71  		// The new buffer would overflow the pipe, so we push a wrapping
    72  		// slot, then try to add the actual slot to the front of the
    73  		// pipe.
    74  		newNext = (newNext & revolutionMask) + jump
    75  		if !t.reclaim(newNext) {
    76  			return nil
    77  		}
    78  		wrappingPayloadSize := slotToPayloadSize(newNext - t.next)
    79  		oldNext := t.next
    80  		t.next = newNext
    81  		if messageAhead {
    82  			t.p.write(oldNext, wrappingPayloadSize)
    83  		} else {
    84  			t.tailHeader = wrappingPayloadSize
    85  			t.Flush()
    86  		}
    87  		return t.Push(payloadSize)
    88  	}
    89  
    90  	// Check that we have enough room for the buffer.
    91  	if !t.reclaim(newNext) {
    92  		return nil
    93  	}
    94  
    95  	if messageAhead {
    96  		t.p.write(t.next, payloadSize)
    97  	} else {
    98  		t.tailHeader = payloadSize
    99  	}
   100  
   101  	// Grab the buffer before updating t.next.
   102  	b := t.p.data(t.next, payloadSize)
   103  	t.next = newNext
   104  
   105  	return b
   106  }
   107  
   108  // reclaim attempts to advance the head until at least newNext. If the head is
   109  // already at or beyond newNext, nothing happens and true is returned; otherwise
   110  // it tries to reclaim slots that have already been consumed by the receive end
   111  // of the pipe (they will be marked as free) and returns a boolean indicating
   112  // whether it was successful in reclaiming enough slots.
   113  func (t *Tx) reclaim(newNext uint64) bool {
   114  	for int64(newNext-t.head) > 0 {
   115  		// Can't reclaim if slot is not free.
   116  		header := t.p.readAtomic(t.head)
   117  		if header&slotFree == 0 {
   118  			return false
   119  		}
   120  
   121  		payloadSize := header & slotSizeMask
   122  		newHead := t.head + payloadToSlotSize(payloadSize)
   123  
   124  		// Check newHead is within bounds and valid.
   125  		if int64(newHead-t.tail) > int64(jump) || newHead&offsetMask >= uint64(len(t.p.buffer)) {
   126  			return false
   127  		}
   128  
   129  		t.head = newHead
   130  	}
   131  
   132  	return true
   133  }
   134  
   135  // Abort causes all Push() calls since the last Flush() to be forgotten and
   136  // therefore they will not be made visible to the receiver.
   137  func (t *Tx) Abort() {
   138  	t.next = t.tail
   139  }
   140  
   141  // Flush causes all buffers pushed since the last Flush() [or Abort(), whichever
   142  // is the most recent] to be made visible to the receiver.
   143  func (t *Tx) Flush() {
   144  	if t.next == t.tail {
   145  		// Nothing to do if there are no pushed buffers.
   146  		return
   147  	}
   148  
   149  	if t.next != t.head {
   150  		// The receiver will spin in t.next, so we must make sure that
   151  		// the slotFree bit is set.
   152  		t.p.write(t.next, slotFree)
   153  	}
   154  
   155  	t.p.writeAtomic(t.tail, t.tailHeader)
   156  	t.tail = t.next
   157  }
   158  
   159  // Bytes returns the byte slice on which the pipe operates.
   160  func (t *Tx) Bytes() []byte {
   161  	return t.p.buffer
   162  }