|
| 1 | +/* Copyright (c) 2020, Stanford University |
| 2 | + * |
| 3 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | + * purpose with or without fee is hereby granted, provided that the above |
| 5 | + * copyright notice and this permission notice appear in all copies. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * @file Homa/Core/Transport.h |
| 18 | + * |
| 19 | + * Contains the low-level Homa Transport API. Advanced users of the Homa |
| 20 | + * Transport library should include this header. |
| 21 | + */ |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include <Homa/Homa.h> |
| 26 | + |
| 27 | +namespace Homa::Core { |
| 28 | + |
| 29 | +/** |
| 30 | + * Minimal set of low-level API that can be used to create Homa-based transports |
| 31 | + * for different runtime environments (e.g. polling, kernel threading, |
| 32 | + * green threads, etc). |
| 33 | + * |
| 34 | + * The execution of a transport is driven through repeated calls to methods |
| 35 | + * like checkTimeouts(), processPacket(), trySend(), and trySendGrants(); the |
| 36 | + * transport will not make any progress otherwise. Advanced users can compose |
| 37 | + * these methods in a way that suits them best. |
| 38 | + * |
| 39 | + * This class is thread-safe. |
| 40 | + */ |
| 41 | +class Transport : public TransportBase { |
| 42 | + public: |
| 43 | + /** |
| 44 | + * Collection of user-defined transport callbacks. |
| 45 | + */ |
| 46 | + class Callbacks { |
| 47 | + public: |
| 48 | + /** |
| 49 | + * Destructor. |
| 50 | + */ |
| 51 | + virtual ~Callbacks() = default; |
| 52 | + |
| 53 | + /** |
| 54 | + * Invoked when an incoming message arrives and needs to dispatched to |
| 55 | + * its destination in the user application for processing. |
| 56 | + * |
| 57 | + * Here are a few example use cases of this callback: |
| 58 | + * <ul> |
| 59 | + * <li> Interaction with the user's thread scheduler: e.g., an |
| 60 | + * application may want to block on receive until a message is |
| 61 | + * delivered, so this method can be used to wake up blocking threads. |
| 62 | + * <li> High-performance message dispatch: e.g., an application may |
| 63 | + * choose to implement the message receive queue with a concurrent MPMC |
| 64 | + * queue as opposed to a linked-list protected by a mutex; <li> |
| 65 | + * Lightweight synchronization: e.g., the socket table that maps from |
| 66 | + * port numbers to sockets is a read-mostly data structure, so lookup |
| 67 | + * operations can benefit from synchronization schemes such as RCU. |
| 68 | + * </ul> |
| 69 | + * |
| 70 | + * @param port |
| 71 | + * Destination port number of the message. |
| 72 | + * @param message |
| 73 | + * Incoming message to dispatch. |
| 74 | + * @return |
| 75 | + * True if the message is delivered successfully; false, otherwise. |
| 76 | + */ |
| 77 | + virtual bool deliver(uint16_t port, InMessage* message) = 0; |
| 78 | + |
| 79 | + /** |
| 80 | + * Invoked when some packets just became ready to be sent (and there was |
| 81 | + * none before). |
| 82 | + * |
| 83 | + * This callback allows the transport library to notify the users that |
| 84 | + * trySend() should be invoked again as soon as possible. For example, |
| 85 | + * the callback can be used to implement wakeup signals for the thread |
| 86 | + * that is responsible for calling trySend(), if this thread decides to |
| 87 | + * sleep when there is no packets to send. |
| 88 | + */ |
| 89 | + virtual void notifySendReady() {} |
| 90 | + }; |
| 91 | + |
| 92 | + /** |
| 93 | + * Return a new instance of a Homa-based transport. |
| 94 | + * |
| 95 | + * @param driver |
| 96 | + * Driver with which this transport should send and receive packets. |
| 97 | + * @param callbacks |
| 98 | + * Collection of user-defined callbacks to customize the behavior of |
| 99 | + * the transport. |
| 100 | + * @param transportId |
| 101 | + * This transport's unique identifier in the group of transports among |
| 102 | + * which this transport will communicate. |
| 103 | + * @return |
| 104 | + * Pointer to the new transport instance. |
| 105 | + */ |
| 106 | + static Homa::unique_ptr<Transport> create(Driver* driver, |
| 107 | + Callbacks* callbacks, |
| 108 | + uint64_t transportId); |
| 109 | + |
| 110 | + /** |
| 111 | + * Process any timeouts that have expired. |
| 112 | + * |
| 113 | + * This method must be called periodically to ensure timely handling of |
| 114 | + * expired timeouts. |
| 115 | + * |
| 116 | + * @return |
| 117 | + * The rdtsc cycle time when this method should be called again. |
| 118 | + */ |
| 119 | + virtual uint64_t checkTimeouts() = 0; |
| 120 | + |
| 121 | + /** |
| 122 | + * Handle an ingress packet by running it through the transport protocol |
| 123 | + * stack. |
| 124 | + * |
| 125 | + * @param packet |
| 126 | + * The ingress packet. |
| 127 | + * @param source |
| 128 | + * IpAddress of the socket from which the packet is sent. |
| 129 | + */ |
| 130 | + virtual void processPacket(Driver::Packet* packet, IpAddress source) = 0; |
| 131 | + |
| 132 | + /** |
| 133 | + * Attempt to send out packets for any messages with unscheduled/granted |
| 134 | + * bytes in a way that limits queue buildup in the NIC. |
| 135 | + * |
| 136 | + * This method must be called eagerly to allow the Transport to make |
| 137 | + * progress toward sending outgoing messages. |
| 138 | + * |
| 139 | + * @param[out] waitUntil |
| 140 | + * The rdtsc cycle time when this method should be called again |
| 141 | + * (this allows the NIC to drain its transmit queue). Only set |
| 142 | + * when this method returns true. |
| 143 | + * @return |
| 144 | + * True if more packets are ready to be transmitted when the method |
| 145 | + * returns; false, otherwise. |
| 146 | + */ |
| 147 | + virtual bool trySend(uint64_t* waitUntil) = 0; |
| 148 | + |
| 149 | + /** |
| 150 | + * Attempt to grant to incoming messages according to the Homa protocol. |
| 151 | + * |
| 152 | + * This method must be called eagerly to allow the Transport to make |
| 153 | + * progress toward receiving incoming messages. |
| 154 | + * |
| 155 | + * @return |
| 156 | + * True if the method has found some messages to grant; false, |
| 157 | + * otherwise. |
| 158 | + */ |
| 159 | + virtual bool trySendGrants() = 0; |
| 160 | +}; |
| 161 | + |
| 162 | +} // namespace Homa::Core |
0 commit comments