github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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 totalLen := payloadToSlotSize(payloadSize) 65 newNext := t.next + totalLen 66 nextWrap := (t.next & revolutionMask) | uint64(len(t.p.buffer)) 67 if int64(newNext-nextWrap) >= 0 { 68 // The new buffer would overflow the pipe, so we push a wrapping 69 // slot, then try to add the actual slot to the front of the 70 // pipe. 71 newNext = (newNext & revolutionMask) + jump 72 wrappingPayloadSize := slotToPayloadSize(newNext - t.next) 73 if !t.reclaim(newNext) { 74 return nil 75 } 76 77 oldNext := t.next 78 t.next = newNext 79 if oldNext != t.tail { 80 t.p.write(oldNext, wrappingPayloadSize) 81 } else { 82 t.tailHeader = wrappingPayloadSize 83 t.Flush() 84 } 85 86 newNext += totalLen 87 } 88 89 // Check that we have enough room for the buffer. 90 if !t.reclaim(newNext) { 91 return nil 92 } 93 94 if t.next != t.tail { 95 t.p.write(t.next, payloadSize) 96 } else { 97 t.tailHeader = payloadSize 98 } 99 100 // Grab the buffer before updating t.next. 101 b := t.p.data(t.next, payloadSize) 102 t.next = newNext 103 104 return b 105 } 106 107 // reclaim attempts to advance the head until at least newNext. If the head is 108 // already at or beyond newNext, nothing happens and true is returned; otherwise 109 // it tries to reclaim slots that have already been consumed by the receive end 110 // of the pipe (they will be marked as free) and returns a boolean indicating 111 // whether it was successful in reclaiming enough slots. 112 func (t *Tx) reclaim(newNext uint64) bool { 113 for int64(newNext-t.head) > 0 { 114 // Can't reclaim if slot is not free. 115 header := t.p.readAtomic(t.head) 116 if header&slotFree == 0 { 117 return false 118 } 119 120 payloadSize := header & slotSizeMask 121 newHead := t.head + payloadToSlotSize(payloadSize) 122 123 // Check newHead is within bounds and valid. 124 if int64(newHead-t.tail) > int64(jump) || newHead&offsetMask >= uint64(len(t.p.buffer)) { 125 return false 126 } 127 128 t.head = newHead 129 } 130 131 return true 132 } 133 134 // Abort causes all Push() calls since the last Flush() to be forgotten and 135 // therefore they will not be made visible to the receiver. 136 func (t *Tx) Abort() { 137 t.next = t.tail 138 } 139 140 // Flush causes all buffers pushed since the last Flush() [or Abort(), whichever 141 // is the most recent] to be made visible to the receiver. 142 func (t *Tx) Flush() { 143 if t.next == t.tail { 144 // Nothing to do if there are no pushed buffers. 145 return 146 } 147 148 if t.next != t.head { 149 // The receiver will spin in t.next, so we must make sure that 150 // the slotFree bit is set. 151 t.p.write(t.next, slotFree) 152 } 153 154 t.p.writeAtomic(t.tail, t.tailHeader) 155 t.tail = t.next 156 } 157 158 // Bytes returns the byte slice on which the pipe operates. 159 func (t *Tx) Bytes() []byte { 160 return t.p.buffer 161 }