1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2018, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * This plugin implements the SDC VXLAN Protocol (SVP).
  18  *
  19  * This plugin is designed to work with a broader distributed system that
  20  * mainains a database of mappings and provides a means of looking up data and
  21  * provides a stream of updates. While it is named after VXLAN, there isn't
  22  * anything specific to VXLAN baked into the protocol at this time, other than
  23  * that it requires both an IP address and a port; however, if there's a good
  24  * reason to support others here, we can modify that.
  25  *
  26  * -----------
  27  * Terminology
  28  * -----------
  29  *
  30  * Throughout this module we refer to a few different kinds of addresses:
  31  *
  32  *    VL3
  33  *
  34  *      A VL3 address, or virtual layer 3, refers to the layer three addreses
  35  *      that are used by entities on an overlay network. As far as we're
  36  *      concerned that means that this is the IP address of an interface on an
  37  *      overlay network.
  38  *
  39  *    VL2
  40  *
  41  *      A VL2 address, or a virtual layer 2, referes to the link-layer addresses
  42  *      that are used by entities on an overlay network. As far as we're
  43  *      concerned that means that this is the MAC addresses of an interface on
  44  *      an overlay network.
  45  *
  46  *    UL3
  47  *
  48  *      A UL3, or underlay layer 3, refers to the layer three (IP) address on
  49  *      the underlay network.
  50  *
  51  * The svp plugin provides lookups from VL3->VL2, eg. the equivalent of an ARP
  52  * or NDP query, and then also provides VL2->UL3 lookups.
  53  *
  54  * -------------------
  55  * Protocol Operations
  56  * -------------------
  57  *
  58  * The svp protocol is defined in lib/varpd/svp/common/libvarpd_svp_prot.h. It
  59  * defines the basic TCP protocol that we use to communicate to hosts. At this
  60  * time, it is not quite 100% implemented in both this plug-in and our primary
  61  * server, sdc-portolan (see https://github.com/joyent/sdc-portolan).
  62  *
  63  * At this time, we don't quite support everything that we need to. Including
  64  * the SVP_R_BULK_REQ and SVP_R_SHOOTDOWN.
  65  *
  66  * ---------------------------------
  67  * General Design and Considerations
  68  * ---------------------------------
  69  *
  70  * Every instance of the svp plugin requires the hostname and port of a server
  71  * to contact. Though, we have co-opted the port 1296 (the year of the oldest
  72  * extant portolan) as our default port.
  73  *
  74  * Each of the different instance of the plugins has a corresponding remote
  75  * backend. The remote backend represents the tuple of the [ host, port ].
  76  * Different instances that share the same host and port tuple will use the same
  77  * backend.
  78  *
  79  * The backend is actually in charge of performing lookups, resolving and
  80  * updating the set of remote hosts based on the DNS resolution we've been
  81  * provided, and taking care of things like shootdowns.
  82  *
  83  * The whole plugin itself maintains an event loop and a number of threads to
  84  * service that event loop. On top of that event loop, we have a simple timer
  85  * backend that ticks at one second intervals and performs various callbacks,
  86  * such as idle query timers, DNS resolution, connection backoff, etc. Each of
  87  * the remote hosts that we obtain is wrapped up in an svp_conn_t, which manages
  88  * the connection state, reconnecting, etc.
  89  *
  90  * All in all, the general way that this all looks like is:
  91  *
  92  *  +----------------------------+
  93  *  | Plugin Instance            |
  94  *  | svp_t                      |
  95  *  |                            |
  96  *  | varpd_provider_handle_t * -+-> varpd handle
  97  *  | uint64_t               ----+-> varpd ID
  98  *  | char *                 ----+-> remote host
  99  *  | uint16_t               ----+-> remote port
 100  *  | svp_remote_t *   ---+------+-> remote backend
 101  *  +---------------------+------+
 102  *                        |
 103  *                        v
 104  *   +----------------------+                   +----------------+
 105  *   | Remote backend       |------------------>| Remove Backend |---> ...
 106  *   | svp_remote_t         |                   | svp_remote_t   |
 107  *   |                      |                   +----------------+
 108  *   | svp_remote_state_t --+-> state flags
 109  *   | svp_degrade_state_t -+-> degraded reason
 110  *   | struct addrinfo *  --+-> resolved hosts
 111  *   | uint_t            ---+-> active hosts
 112  *   | uint_t            ---+-> DNS generation
 113  *   | uint_t            ---+-> Reference count
 114  *   | uint_t            ---+-> active conns
 115  *   | uint_t            ---+-> degraded conns
 116  *   | list_t        ---+---+-> connection list
 117  *   +------------------+---+
 118  *                      |
 119  *                      +------------------------------+-----------------+
 120  *                      |                              |                 |
 121  *                      v                              v                 v
 122  *   +-------------------+                       +----------------
 123  *   | SVP Connection    |                       | SVP connection |     ...
 124  *   | svp_conn_t        |                       | svp_conn_t     |
 125  *   |                   |                       +----------------+
 126  *   | svp_event_t   ----+-> event loop handle
 127  *   | svp_timer_t   ----+-> backoff timer
 128  *   | svp_timer_t   ----+-> query timer
 129  *   | int           ----+-> socket fd
 130  *   | uint_t        ----+-> generation
 131  *   | uint_t        ----+-> current backoff
 132  *   | svp_conn_flags_t -+-> connection flags
 133  *   | svp_conn_state_t -+-> connection state
 134  *   | svp_conn_error_t -+-> connection error
 135  *   | int            ---+-> last errrno
 136  *   | hrtime_t       ---+-> activity timestamp
 137  *   | svp_conn_out_t ---+-> outgoing data state
 138  *   | svp_conn_in_t  ---+-> incoming data state
 139  *   | list_t      ---+--+-> active queries
 140  *   +----------------+--+
 141  *                    |
 142  *                    +----------------------------------+-----------------+
 143  *                    |                                  |                 |
 144  *                    v                                  v                 v
 145  *   +--------------------+                       +-------------+
 146  *   | SVP Query          |                       | SVP Query   |         ...
 147  *   | svp_query_t        |                       | svp_query_t |
 148  *   |                    |                       +-------------+
 149  *   | svp_query_f     ---+-> callback function
 150  *   | void *          ---+-> callback arg
 151  *   | svp_query_state_t -+-> state flags
 152  *   | svp_req_t       ---+-> svp prot. header
 153  *   | svp_query_data_t --+-> read data
 154  *   | svp_query_data_t --+-> write data
 155  *   | svp_status_t    ---+-> request status
 156  *   +--------------------+
 157  *
 158  * The svp_t is the instance that we assoicate with varpd. The instance itself
 159  * maintains properties and then when it's started associates with an
 160  * svp_remote_t, which is the remote backend. The remote backend itself,
 161  * maintains the DNS state and spins up and downs connections based on the
 162  * results from DNS. By default, we query DNS every 30 seconds. For more on the
 163  * connection life cycle, see the next section.
 164  *
 165  * By default, each connection maintains its own back off timer and list of
 166  * queries it's servicing. Only one request is generally outstanding at a time
 167  * and requests are round robined across the various connections.
 168  *
 169  * The query itself represents the svp request that's going on and keep track of
 170  * its state and is a place for data that's read and written to as part of the
 171  * request.
 172  *
 173  * Connections maintain a query timer such that if we have not received data on
 174  * a socket for a certain amount of time, we kill that socket and begin a
 175  * reconnection cycle with backoff.
 176  *
 177  * ------------------------
 178  * Connection State Machine
 179  * ------------------------
 180  *
 181  * We have a connection pool that's built upon DNS records. DNS describes the
 182  * membership of the set of remote peers that make up our pool and we maintain
 183  * one connection to each of them.  In addition, we maintain an exponential
 184  * backoff for each peer and will attempt to reconect immediately before backing
 185  * off. The following are the valid states that a connection can be in:
 186  *
 187  *      SVP_CS_ERROR            An OS error has occurred on this connection,
 188  *                              such as failure to create a socket or associate
 189  *                              the socket with an event port. We also
 190  *                              transition all connections to this state before
 191  *                              we destroy them.
 192  *
 193  *      SVP_CS_INITIAL          This is the initial state of a connection, all
 194  *                              that should exist is an unbound socket.
 195  *
 196  *      SVP_CS_CONNECTING       A call to connect has been made and we are
 197  *                              polling for it to complete.
 198  *
 199  *      SVP_CS_BACKOFF          A connect attempt has failed and we are
 200  *                              currently backing off, waiting to try again.
 201  *
 202  *      SVP_CS_ACTIVE           We have successfully connected to the remote
 203  *                              system.
 204  *
 205  *      SVP_CS_WINDDOWN         This connection is going to valhalla. In other
 206  *                              words, a previously active connection is no
 207  *                              longer valid in DNS, so we should curb our use
 208  *                              of it, and reap it as soon as we have other
 209  *                              active connections.
 210  *
 211  * The following diagram attempts to describe our state transition scheme, and
 212  * when we transition from one state to the next.
 213  *
 214  *                               |
 215  *                               * New remote IP from DNS resolution,
 216  *                               | not currently active in the system.
 217  *                               |
 218  *                               v                                Socket Error,
 219  *                       +----------------+                       still in DNS
 220  *  +----------------<---| SVP_CS_INITIAL |<----------------------*-----+
 221  *  |                    +----------------+                             |
 222  *  |                            System  |                              |
 223  *  | Connection . . . . .       success *               Successful     |
 224  *  | failed             .               |               connect()      |
 225  *  |               +----*---------+     |        +-----------*--+      |
 226  *  |               |              |     |        |              |      |
 227  *  |               V              ^     v        ^              V      ^
 228  *  |  +----------------+         +-------------------+     +---------------+
 229  *  +<-| SVP_CS_BACKOFF |         | SVP_CS_CONNECTING |     | SVP_CS_ACTIVE |
 230  *  |  +----------------+         +-------------------+     +---------------+
 231  *  |               V              ^  V                       V  V
 232  *  | Backoff wait  *              |  |                       |  * Removed
 233  *  v interval      +--------------+  +-----------------<-----+  | from DNS
 234  *  | finished                        |                          |
 235  *  |                                 V                          |
 236  *  |                                 |                          V
 237  *  |                                 |            +-----------------+
 238  *  +----------------+----------<-----+-------<----| SVP_CS_WINDDOWN |
 239  *                   |                             +-----------------+
 240  *                   * . . .   Fatal system, not
 241  *                   |         socket error or
 242  *                   V         quiesced after
 243  *           +--------------+  removal from DNS
 244  *           | SVP_CS_ERROR |
 245  *           +--------------+
 246  *                   |
 247  *                   * . . . Removed from DNS
 248  *                   v
 249  *            +------------+
 250  *            | Connection |
 251  *            | Destroyed  |
 252  *            +------------+
 253  *
 254  * --------------------------
 255  * Connection Event Injection
 256  * --------------------------
 257  *
 258  * For each connection that exists in the system, we have a timer in place that
 259  * is in charge of performing timeout activity. It fires once every thirty
 260  * seconds or so for a given connection and checks to ensure that we have had
 261  * activity for the most recent query on the connection. If not, it terminates
 262  * the connection. This is important as if we have sent all our data and are
 263  * waiting for the remote end to reply, without enabling something like TCP
 264  * keep-alive, we will not be notified that anything that has happened to the
 265  * remote connection, for example a panic. In addition, this also protects
 266  * against a server that is up, but a portolan that is not making forward
 267  * progress.
 268  *
 269  * When a timeout occurs, we first try to disassociate any active events, which
 270  * by definition must exist. Once that's done, we inject a port source user
 271  * event. Now, there is a small gotcha. Let's assume for a moment that we have a
 272  * pathological portolan. That means that it knows to inject activity right at
 273  * the time out window. That means, that the event may be disassociated before
 274  * we could get to it. If that's the case, we must _not_ inject the user event
 275  * and instead, we'll let the pending event take care of it. We know that the
 276  * pending event hasn't hit the main part of the loop yet, otherwise, it would
 277  * have released the lock protecting our state and associated the event.
 278  *
 279  * ------------
 280  * Notes on DNS
 281  * ------------
 282  *
 283  * Unfortunately, doing host name resolution in a way that allows us to leverage
 284  * the system's resolvers and the system's caching, require us to make blocking
 285  * calls in libc via getaddrinfo(3SOCKET). If we can't reach a given server,
 286  * that will tie up a thread for quite some time. To work around that fact,
 287  * we're going to create a fixed number of threads and we'll use them to service
 288  * our DNS requests. While this isn't ideal, until we have a sane means of
 289  * integrating a DNS resolution into an event loop with say portfs, it's not
 290  * going to be a fun day no matter what we do.
 291  *
 292  * ------
 293  * Timers
 294  * ------
 295  *
 296  * We maintain a single timer based on CLOCK_REALTIME. It's designed to fire
 297  * every second. While we'd rather use CLOCK_HIGHRES just to alleviate ourselves
 298  * from timer drift; however, as zones may not actually have CLOCK_HIGHRES
 299  * access, we don't want them to end up in there. The timer itself is just a
 300  * simple avl tree sorted by expiration time, which is stored as a tick in the
 301  * future, a tick is just one second.
 302  *
 303  * ----------
 304  * Shootdowns
 305  * ----------
 306  *
 307  * As part of the protocol, we need to be able to handle shootdowns that inform
 308  * us some of the information in the system is out of date. This information
 309  * needs to be processed promptly; however, the information is hopefully going
 310  * to be relatively infrequent relative to the normal flow of information.
 311  *
 312  * The shoot down information needs to be done on a per-backend basis. The
 313  * general design is that we'll have a single query for this which can fire on a
 314  * 5-10s period, we randmoize the latter part to give us a bit more load
 315  * spreading. If we complete because there's no work to do, then we wait the
 316  * normal period. If we complete, but there's still work to do, we'll go again
 317  * after a second.
 318  *
 319  * A shootdown has a few different parts. We first receive a list of items to
 320  * shootdown. After performing all of those, we need to acknowledge them. When
 321  * that's been done successfully, we can move onto the next part. From a
 322  * protocol perspective, we make a SVP_R_LOG_REQ, we get a reply, and then after
 323  * processing them, send an SVP_R_LOG_RM. Only once that's been acked do we
 324  * continue.
 325  *
 326  * However, one of the challenges that we have is that these invalidations are
 327  * just that, an invalidation. For a virtual layer two request, that's fine,
 328  * because the kernel supports that. However, for virtual layer three
 329  * invalidations, we have a bit more work to do. These protocols, ARP and NDP,
 330  * don't really support a notion of just an invalidation, instead you have to
 331  * inject the new data in a gratuitous fashion.
 332  *
 333  * To that end, what we instead do is when we receive a VL3 invalidation, we
 334  * turn that info a VL3 request. We hold the general request as outstanding
 335  * until we receive all of the callbacks for the VL3 invalidations, at which
 336  * point we go through and do the log removal request.
 337  */
 338 
 339 #include <umem.h>
 340 #include <errno.h>
 341 #include <stdlib.h>
 342 #include <sys/types.h>
 343 #include <sys/socket.h>
 344 #include <netinet/in.h>
 345 #include <arpa/inet.h>
 346 #include <libnvpair.h>
 347 #include <strings.h>
 348 #include <string.h>
 349 #include <assert.h>
 350 #include <unistd.h>
 351 
 352 #include <libvarpd_provider.h>
 353 #include "libvarpd_svp.h"
 354 
 355 bunyan_logger_t *svp_bunyan;
 356 static int svp_defport = 1296;
 357 static int svp_defuport = 1339;
 358 static umem_cache_t *svp_lookup_cache;
 359 
 360 typedef enum svp_lookup_type {
 361         SVP_L_UNKNOWN   = 0x0,
 362         SVP_L_VL2       = 0x1,
 363         SVP_L_VL3       = 0x2,
 364         SVP_L_ROUTE     = 0x3
 365 } svp_lookup_type_t;
 366 
 367 typedef struct svp_lookup {
 368         int svl_type;
 369         union {
 370                 struct svl_lookup_vl2 {
 371                         varpd_query_handle_t    *svl_handle;
 372                         overlay_target_point_t  *svl_point;
 373                 } svl_vl2;
 374                 struct svl_lookup_vl3 {
 375                         varpd_arp_handle_t      *svl_vah;
 376                         uint8_t                 *svl_out;
 377                 } svl_vl3;
 378                 struct svl_lookup_route {
 379                         varpd_query_handle_t    *svl_handle;
 380                         overlay_target_point_t  *svl_point;
 381                         overlay_target_route_t  *svl_route;
 382                         overlay_target_mac_t    *svl_mac;
 383                 } svl_route;
 384         } svl_u;
 385         svp_query_t                             svl_query;
 386 } svp_lookup_t;
 387 
 388 static const char *varpd_svp_props[] = {
 389         "svp/host",
 390         "svp/port",
 391         "svp/underlay_ip",
 392         "svp/underlay_port",
 393         "svp/dcid",
 394         "svp/router_oui"
 395 };
 396 
 397 static const uint8_t svp_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 398 
 399 int
 400 svp_comparator(const void *l, const void *r)
 401 {
 402         const svp_t *ls = l;
 403         const svp_t *rs = r;
 404 
 405         if (ls->svp_vid > rs->svp_vid)
 406                 return (1);
 407         if (ls->svp_vid < rs->svp_vid)
 408                 return (-1);
 409         return (0);
 410 }
 411 
 412 static void
 413 svp_vl2_lookup_cb(svp_t *svp, svp_status_t status, const struct in6_addr *uip,
 414     const uint16_t uport, void *arg)
 415 {
 416         svp_lookup_t *svl = arg;
 417         overlay_target_point_t *otp;
 418 
 419         assert(svp != NULL);
 420         assert(arg != NULL);
 421 
 422         if (status != SVP_S_OK) {
 423                 libvarpd_plugin_query_reply(svl->svl_u.svl_vl2.svl_handle,
 424                     VARPD_LOOKUP_DROP);
 425                 umem_cache_free(svp_lookup_cache, svl);
 426                 return;
 427         }
 428 
 429         otp = svl->svl_u.svl_vl2.svl_point;
 430         bcopy(uip, &otp->otp_ip, sizeof (struct in6_addr));
 431         otp->otp_port = uport;
 432         libvarpd_plugin_query_reply(svl->svl_u.svl_vl2.svl_handle,
 433             VARPD_LOOKUP_OK);
 434         umem_cache_free(svp_lookup_cache, svl);
 435 }
 436 
 437 static void
 438 svp_vl3_lookup_cb(svp_t *svp, svp_status_t status, const uint8_t *vl2mac,
 439     const struct in6_addr *uip, const uint16_t uport, void *arg)
 440 {
 441         /* Initialize address-holders to 0 for comparisons-to-zeroes later. */
 442         overlay_target_point_t point = { 0 };
 443         svp_lookup_t *svl = arg;
 444         uint8_t nexthop_mac[6] = { 0, 0, 0, 0, 0, 0 };
 445 
 446         assert(svp != NULL);
 447         assert(svl != NULL);
 448 
 449         if (status != SVP_S_OK) {
 450                 libvarpd_plugin_arp_reply(svl->svl_u.svl_vl3.svl_vah,
 451                     VARPD_LOOKUP_DROP);
 452                 umem_cache_free(svp_lookup_cache, svl);
 453                 return;
 454         }
 455 
 456         /* Inject the L2 mapping before the L3 */
 457         if (uport != 0 &&
 458             bcmp(uip, &point.otp_ip, sizeof (struct in6_addr)) != 0) {
 459                 /* Normal L3 lookup result... */
 460                 bcopy(uip, &point.otp_ip, sizeof (struct in6_addr));
 461                 point.otp_port = uport;
 462                 libvarpd_inject_varp(svp->svp_hdl, vl2mac, &point);
 463         } else {
 464                 /*
 465                  * Oh my, we have a next-hop router IP.
 466                  * Set the MAC to the ouid+vid concatenated
 467                  * special-router-MAC. Overlay down below will know
 468                  * that uport == 0 means the MAC is a special one.
 469                  */
 470                 if (bcmp(svp->svp_router_oui, nexthop_mac, ETHERADDRL) == 0) {
 471                         /*
 472                          * We don't have a router_oui, so we can't support
 473                          * special-router-MAC.  Drop it.
 474                          */
 475                         libvarpd_plugin_arp_reply(svl->svl_u.svl_vl3.svl_vah,
 476                             VARPD_LOOKUP_DROP);
 477                         umem_cache_free(svp_lookup_cache, svl);
 478                         return;
 479                 }
 480                 bcopy(svp->svp_router_oui, nexthop_mac, 3);
 481                 nexthop_mac[3] = (svp->svp_vid >> 16) & 0xff;
 482                 nexthop_mac[4] = (svp->svp_vid >> 8) & 0xff;
 483                 nexthop_mac[5] = svp->svp_vid & 0xff;
 484                 vl2mac = nexthop_mac;
 485         }
 486 
 487         bcopy(vl2mac, svl->svl_u.svl_vl3.svl_out, ETHERADDRL);
 488         libvarpd_plugin_arp_reply(svl->svl_u.svl_vl3.svl_vah,
 489             VARPD_LOOKUP_OK);
 490         umem_cache_free(svp_lookup_cache, svl);
 491 }
 492 
 493 static void
 494 svp_vl2_invalidate_cb(svp_t *svp, const uint8_t *vl2mac)
 495 {
 496         libvarpd_inject_varp(svp->svp_hdl, vl2mac, NULL);
 497 }
 498 
 499 static void
 500 svp_vl3_inject_cb(svp_t *svp, const uint16_t vlan, const struct in6_addr *vl3ip,
 501     const uint8_t *vl2mac, const uint8_t *targmac)
 502 {
 503         struct in_addr v4;
 504 
 505         /*
 506          * At the moment we don't support any IPv6 related log entries, this
 507          * will change soon as we develop a bit more of the IPv6 related
 508          * infrastructure so we can properly test the injection.
 509          */
 510         if (IN6_IS_ADDR_V4MAPPED(vl3ip) == 0) {
 511                 return;
 512         } else {
 513                 IN6_V4MAPPED_TO_INADDR(vl3ip, &v4);
 514                 if (targmac == NULL)
 515                         targmac = svp_bcast;
 516                 libvarpd_inject_arp(svp->svp_hdl, vlan, vl2mac, &v4, targmac);
 517         }
 518 }
 519 
 520 /* ARGSUSED */
 521 static void
 522 svp_shootdown_cb(svp_t *svp, const uint8_t *vl2mac, const struct in6_addr *uip,
 523     const uint16_t uport)
 524 {
 525         /*
 526          * We should probably do a conditional invalidation here.
 527          */
 528         libvarpd_inject_varp(svp->svp_hdl, vl2mac, NULL);
 529 }
 530 
 531 static void
 532 svp_route_lookup_cb(svp_t *svp, svp_status_t status, uint32_t dcid,
 533     uint32_t vnetid, uint16_t vlan, uint8_t *srcmac, uint8_t *dstmac,
 534     uint16_t ul3_port, uint8_t *ul3_addr, uint8_t srcpfx, uint8_t dstpfx,
 535     void *arg)
 536 {
 537         svp_lookup_t *svl = arg;
 538         overlay_target_point_t *otp;
 539         overlay_target_route_t *otr;
 540         overlay_target_mac_t *otm;
 541 
 542         if (status != SVP_S_OK) {
 543                 libvarpd_plugin_query_reply(svl->svl_u.svl_route.svl_handle,
 544                     VARPD_LOOKUP_DROP);
 545                 umem_cache_free(svp_lookup_cache, svl);
 546                 return;
 547         }
 548 
 549         otp = svl->svl_u.svl_route.svl_point;
 550         bcopy(ul3_addr, &otp->otp_ip, sizeof (struct in6_addr));
 551         otp->otp_port = ul3_port;
 552 
 553         otr = svl->svl_u.svl_route.svl_route;
 554         otr->otr_vnet = vnetid;
 555         otr->otr_vlan = vlan;
 556         bcopy(srcmac, otr->otr_srcmac, ETHERADDRL);
 557 
 558         otm = svl->svl_u.svl_route.svl_mac;
 559         otm->otm_dcid = dcid;
 560         bcopy(dstmac, otm->otm_mac, ETHERADDRL);
 561 
 562         libvarpd_plugin_query_reply(svl->svl_u.svl_route.svl_handle,
 563             VARPD_LOOKUP_OK);
 564         umem_cache_free(svp_lookup_cache, svl);
 565 }
 566 
 567 static svp_cb_t svp_defops = {
 568         svp_vl2_lookup_cb,
 569         svp_vl3_lookup_cb,
 570         svp_vl2_invalidate_cb,
 571         svp_vl3_inject_cb,
 572         svp_shootdown_cb,
 573         svp_route_lookup_cb,
 574 };
 575 
 576 static boolean_t
 577 varpd_svp_valid_dest(overlay_plugin_dest_t dest)
 578 {
 579         if (dest != (OVERLAY_PLUGIN_D_IP | OVERLAY_PLUGIN_D_PORT))
 580                 return (B_FALSE);
 581 
 582         return (B_TRUE);
 583 }
 584 
 585 static int
 586 varpd_svp_create(varpd_provider_handle_t *hdl, void **outp,
 587     overlay_plugin_dest_t dest)
 588 {
 589         int ret;
 590         svp_t *svp;
 591 
 592         if (varpd_svp_valid_dest(dest) == B_FALSE)
 593                 return (ENOTSUP);
 594 
 595         svp = umem_zalloc(sizeof (svp_t), UMEM_DEFAULT);
 596         if (svp == NULL)
 597                 return (ENOMEM);
 598 
 599         if ((ret = mutex_init(&svp->svp_lock, USYNC_THREAD | LOCK_ERRORCHECK,
 600             NULL)) != 0) {
 601                 umem_free(svp, sizeof (svp_t));
 602                 return (ret);
 603         }
 604 
 605         svp->svp_port = svp_defport;
 606         svp->svp_uport = svp_defuport;
 607         svp->svp_cb = svp_defops;
 608         svp->svp_hdl = hdl;
 609         svp->svp_vid = libvarpd_plugin_vnetid(svp->svp_hdl);
 610         *outp = svp;
 611         return (0);
 612 }
 613 
 614 static int
 615 varpd_svp_start(void *arg)
 616 {
 617         int ret;
 618         svp_remote_t *srp;
 619         svp_t *svp = arg;
 620 
 621         mutex_enter(&svp->svp_lock);
 622         if (svp->svp_host == NULL || svp->svp_port == 0 ||
 623             svp->svp_huip == B_FALSE || svp->svp_uport == 0) {
 624                 mutex_exit(&svp->svp_lock);
 625                 return (EAGAIN);
 626         }
 627         mutex_exit(&svp->svp_lock);
 628 
 629         if ((ret = svp_remote_find(svp->svp_host, svp->svp_port, &svp->svp_uip,
 630             &srp)) != 0)
 631                 return (ret);
 632 
 633         if ((ret = svp_remote_attach(srp, svp)) != 0) {
 634                 svp_remote_release(srp);
 635                 return (ret);
 636         }
 637 
 638         return (0);
 639 }
 640 
 641 static void
 642 varpd_svp_stop(void *arg)
 643 {
 644         svp_t *svp = arg;
 645 
 646         svp_remote_detach(svp);
 647 }
 648 
 649 static void
 650 varpd_svp_destroy(void *arg)
 651 {
 652         svp_t *svp = arg;
 653 
 654         if (svp->svp_host != NULL)
 655                 umem_free(svp->svp_host, strlen(svp->svp_host) + 1);
 656 
 657         if (mutex_destroy(&svp->svp_lock) != 0)
 658                 libvarpd_panic("failed to destroy svp_t`svp_lock");
 659 
 660         umem_free(svp, sizeof (svp_t));
 661 }
 662 
 663 static void
 664 varpd_svp_lookup_l3(svp_t *svp, varpd_query_handle_t *vqh,
 665     const overlay_targ_lookup_t *otl, overlay_target_point_t *otp,
 666     overlay_target_route_t *otr, overlay_target_mac_t *otm)
 667 {
 668         svp_lookup_t *slp;
 669         uint32_t type;
 670         const struct in6_addr *src = &otl->otl_addru.otlu_l3.otl3_srcip,
 671             *dst = &otl->otl_addru.otlu_l3.otl3_dstip;
 672 
 673         /*
 674          * otl is an L3 request, so we have src/dst IPs for the inner packet.
 675          * We also have the vlan.
 676          *
 677          * Assume kernel's overlay module is caching well, so we are directly
 678          * going to query (i.e. no caching up here of actual destinations).
 679          *
 680          * Our existing remote sever (svp_remote), but with the new message
 681          * SVP_R_ROUTE_REQ.
 682          */
 683 
 684         if (IN6_IS_ADDR_V4MAPPED(src)) {
 685                 if (!IN6_IS_ADDR_V4MAPPED(dst)) {
 686                         libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 687                         return;
 688                 }
 689                 type = SVP_VL3_IP;
 690         } else {
 691                 if (IN6_IS_ADDR_V4MAPPED(dst)) {
 692                         libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 693                         return;
 694                 }
 695                 type = SVP_VL3_IPV6;
 696         }
 697 
 698         slp = umem_cache_alloc(svp_lookup_cache, UMEM_DEFAULT);
 699         if (slp == NULL) {
 700                 libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 701                 return;
 702         }
 703 
 704         slp->svl_type = SVP_L_ROUTE;
 705         slp->svl_u.svl_route.svl_handle = vqh;
 706         slp->svl_u.svl_route.svl_point = otp;
 707         slp->svl_u.svl_route.svl_route = otr;
 708         slp->svl_u.svl_route.svl_mac = otm;
 709 
 710         svp_remote_route_lookup(svp, &slp->svl_query, src, dst,
 711             otl->otl_vnetid, (uint16_t)otl->otl_vlan, slp);
 712 }
 713 
 714 static void
 715 varpd_svp_lookup(void *arg, varpd_query_handle_t *vqh,
 716     const overlay_targ_lookup_t *otl, overlay_target_point_t *otp,
 717     overlay_target_route_t *otr, overlay_target_mac_t *otm)
 718 {
 719         svp_lookup_t *slp;
 720         svp_t *svp = arg;
 721 
 722         /*
 723          * Shuffle off L3 lookups to their own codepath.
 724          */
 725         if (otl->otl_l3req) {
 726                 varpd_svp_lookup_l3(svp, vqh, otl, otp, otr, otm);
 727                 return;
 728         }
 729         /*
 730          * At this point, the traditional overlay_target_point_t is all that
 731          * needs filling in.  Zero-out the otr for safety.
 732          */
 733         bzero(otr, sizeof (*otr));
 734 
 735 
 736         /*
 737          * Check if this is something that we need to proxy, eg. arp or ndp.
 738          */
 739         if (otl->otl_addru.otlu_l2.otl2_sap == ETHERTYPE_ARP) {
 740                 libvarpd_plugin_proxy_arp(svp->svp_hdl, vqh, otl);
 741                 return;
 742         }
 743 
 744         if (otl->otl_addru.otlu_l2.otl2_dstaddr[0] == 0x33 &&
 745             otl->otl_addru.otlu_l2.otl2_dstaddr[1] == 0x33) {
 746                 if (otl->otl_addru.otlu_l2.otl2_sap == ETHERTYPE_IPV6) {
 747                         libvarpd_plugin_proxy_ndp(svp->svp_hdl, vqh, otl);
 748                 } else {
 749                         libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 750                 }
 751                 return;
 752         }
 753 
 754         /*
 755          * Watch out for various multicast and broadcast addresses. We've
 756          * already taken care of the IPv6 range above. Now we just need to
 757          * handle broadcast and if the multicast bit is set, lowest bit of the
 758          * first octet of the MAC, then we drop it now.
 759          */
 760         if (bcmp(otl->otl_addru.otlu_l2.otl2_dstaddr, svp_bcast,
 761             ETHERADDRL) == 0 ||
 762             (otl->otl_addru.otlu_l2.otl2_dstaddr[0] & 0x01) == 0x01) {
 763                 libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 764                 return;
 765         }
 766 
 767         /*
 768          * If we have a failure to allocate memory for this, that's not good.
 769          * However, telling the kernel to just drop this packet is much better
 770          * than the alternative at this moment. At least we'll try again and we
 771          * may have something more available to us in a little bit.
 772          */
 773         slp = umem_cache_alloc(svp_lookup_cache, UMEM_DEFAULT);
 774         if (slp == NULL) {
 775                 libvarpd_plugin_query_reply(vqh, VARPD_LOOKUP_DROP);
 776                 return;
 777         }
 778 
 779         slp->svl_type = SVP_L_VL2;
 780         slp->svl_u.svl_vl2.svl_handle = vqh;
 781         slp->svl_u.svl_vl2.svl_point = otp;
 782 
 783         svp_remote_vl2_lookup(svp, &slp->svl_query,
 784             otl->otl_addru.otlu_l2.otl2_dstaddr, slp);
 785 }
 786 
 787 /* ARGSUSED */
 788 static int
 789 varpd_svp_nprops(void *arg, uint_t *nprops)
 790 {
 791         *nprops = sizeof (varpd_svp_props) / sizeof (char *);
 792         return (0);
 793 }
 794 
 795 /* ARGSUSED */
 796 static int
 797 varpd_svp_propinfo(void *arg, uint_t propid, varpd_prop_handle_t *vph)
 798 {
 799         switch (propid) {
 800         case 0:
 801                 /* svp/host */
 802                 libvarpd_prop_set_name(vph, varpd_svp_props[0]);
 803                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 804                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_STRING);
 805                 libvarpd_prop_set_nodefault(vph);
 806                 break;
 807         case 1:
 808                 /* svp/port */
 809                 libvarpd_prop_set_name(vph, varpd_svp_props[1]);
 810                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 811                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_UINT);
 812                 (void) libvarpd_prop_set_default(vph, &svp_defport,
 813                     sizeof (svp_defport));
 814                 libvarpd_prop_set_range_uint32(vph, 1, UINT16_MAX);
 815                 break;
 816         case 2:
 817                 /* svp/underlay_ip */
 818                 libvarpd_prop_set_name(vph, varpd_svp_props[2]);
 819                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 820                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_IP);
 821                 libvarpd_prop_set_nodefault(vph);
 822                 break;
 823         case 3:
 824                 /* svp/underlay_port */
 825                 libvarpd_prop_set_name(vph, varpd_svp_props[3]);
 826                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 827                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_UINT);
 828                 (void) libvarpd_prop_set_default(vph, &svp_defuport,
 829                     sizeof (svp_defuport));
 830                 libvarpd_prop_set_range_uint32(vph, 1, UINT16_MAX);
 831                 break;
 832         case 4:
 833                 /* svp/dcid */
 834                 libvarpd_prop_set_name(vph, varpd_svp_props[4]);
 835                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 836                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_UINT);
 837                 libvarpd_prop_set_nodefault(vph);
 838                 libvarpd_prop_set_range_uint32(vph, 1, UINT32_MAX - 1);
 839                 break;
 840         case 5:
 841                 /* svp/router_oui */
 842                 libvarpd_prop_set_name(vph, varpd_svp_props[5]);
 843                 libvarpd_prop_set_prot(vph, OVERLAY_PROP_PERM_RRW);
 844                 libvarpd_prop_set_type(vph, OVERLAY_PROP_T_ETHER);
 845                 libvarpd_prop_set_nodefault(vph);
 846                 break;
 847         default:
 848                 return (EINVAL);
 849         }
 850         return (0);
 851 }
 852 
 853 static int
 854 varpd_svp_getprop(void *arg, const char *pname, void *buf, uint32_t *sizep)
 855 {
 856         svp_t *svp = arg;
 857 
 858         /* svp/host */
 859         if (strcmp(pname, varpd_svp_props[0]) == 0) {
 860                 size_t len;
 861 
 862                 mutex_enter(&svp->svp_lock);
 863                 if (svp->svp_host == NULL) {
 864                         *sizep = 0;
 865                 } else {
 866                         len = strlen(svp->svp_host) + 1;
 867                         if (*sizep < len) {
 868                                 mutex_exit(&svp->svp_lock);
 869                                 return (EOVERFLOW);
 870                         }
 871                         *sizep = len;
 872                         (void) strlcpy(buf, svp->svp_host, *sizep);
 873                 }
 874                 mutex_exit(&svp->svp_lock);
 875                 return (0);
 876         }
 877 
 878         /* svp/port */
 879         if (strcmp(pname, varpd_svp_props[1]) == 0) {
 880                 uint64_t val;
 881 
 882                 if (*sizep < sizeof (uint64_t))
 883                         return (EOVERFLOW);
 884 
 885                 mutex_enter(&svp->svp_lock);
 886                 if (svp->svp_port == 0) {
 887                         *sizep = 0;
 888                 } else {
 889                         val = svp->svp_port;
 890                         bcopy(&val, buf, sizeof (uint64_t));
 891                         *sizep = sizeof (uint64_t);
 892                 }
 893                 mutex_exit(&svp->svp_lock);
 894                 return (0);
 895         }
 896 
 897         /* svp/underlay_ip */
 898         if (strcmp(pname, varpd_svp_props[2]) == 0) {
 899                 if (*sizep < sizeof (struct in6_addr))
 900                         return (EOVERFLOW);
 901                 mutex_enter(&svp->svp_lock);
 902                 if (svp->svp_huip == B_FALSE) {
 903                         *sizep = 0;
 904                 } else {
 905                         bcopy(&svp->svp_uip, buf, sizeof (struct in6_addr));
 906                         *sizep = sizeof (struct in6_addr);
 907                 }
 908                 mutex_exit(&svp->svp_lock);
 909                 return (0);
 910         }
 911 
 912         /* svp/underlay_port */
 913         if (strcmp(pname, varpd_svp_props[3]) == 0) {
 914                 uint64_t val;
 915 
 916                 if (*sizep < sizeof (uint64_t))
 917                         return (EOVERFLOW);
 918 
 919                 mutex_enter(&svp->svp_lock);
 920                 if (svp->svp_uport == 0) {
 921                         *sizep = 0;
 922                 } else {
 923                         val = svp->svp_uport;
 924                         bcopy(&val, buf, sizeof (uint64_t));
 925                         *sizep = sizeof (uint64_t);
 926                 }
 927 
 928                 mutex_exit(&svp->svp_lock);
 929                 return (0);
 930         }
 931 
 932         /* svp/dcid */
 933         if (strcmp(pname, varpd_svp_props[4]) == 0) {
 934                 uint64_t val;
 935 
 936                 if (*sizep < sizeof (uint64_t))
 937                         return (EOVERFLOW);
 938 
 939                 mutex_enter(&svp->svp_lock);
 940                 if (svp->svp_uport == 0) {
 941                         *sizep = 0;
 942                 } else {
 943                         val = svp->svp_dcid;
 944                         bcopy(&val, buf, sizeof (uint64_t));
 945                         *sizep = sizeof (uint64_t);
 946                 }
 947 
 948                 mutex_exit(&svp->svp_lock);
 949                 return (0);
 950         }
 951 
 952         /* svp/router_oui */
 953         if (strcmp(pname, varpd_svp_props[5]) == 0) {
 954                 if (*sizep < ETHERADDRL)
 955                         return (EOVERFLOW);
 956                 mutex_enter(&svp->svp_lock);
 957 
 958                 if (ether_is_zero(&svp->svp_router_oui)) {
 959                         *sizep = 0;
 960                 } else {
 961                         bcopy(&svp->svp_router_oui, buf, ETHERADDRL);
 962                         *sizep = ETHERADDRL;
 963                 }
 964 
 965                 mutex_exit(&svp->svp_lock);
 966                 return (0);
 967         }
 968         return (EINVAL);
 969 }
 970 
 971 static int
 972 varpd_svp_setprop(void *arg, const char *pname, const void *buf,
 973     const uint32_t size)
 974 {
 975         svp_t *svp = arg;
 976 
 977         /* svp/host */
 978         if (strcmp(pname, varpd_svp_props[0]) == 0) {
 979                 char *dup;
 980                 dup = umem_alloc(size, UMEM_DEFAULT);
 981                 (void) strlcpy(dup, buf, size);
 982                 if (dup == NULL)
 983                         return (ENOMEM);
 984                 mutex_enter(&svp->svp_lock);
 985                 if (svp->svp_host != NULL)
 986                         umem_free(svp->svp_host, strlen(svp->svp_host) + 1);
 987                 svp->svp_host = dup;
 988                 mutex_exit(&svp->svp_lock);
 989                 return (0);
 990         }
 991 
 992         /* svp/port */
 993         if (strcmp(pname, varpd_svp_props[1]) == 0) {
 994                 const uint64_t *valp = buf;
 995                 if (size < sizeof (uint64_t))
 996                         return (EOVERFLOW);
 997 
 998                 if (*valp == 0 || *valp > UINT16_MAX)
 999                         return (EINVAL);
1000 
1001                 mutex_enter(&svp->svp_lock);
1002                 svp->svp_port = (uint16_t)*valp;
1003                 mutex_exit(&svp->svp_lock);
1004                 return (0);
1005         }
1006 
1007         /* svp/underlay_ip */
1008         if (strcmp(pname, varpd_svp_props[2]) == 0) {
1009                 const struct in6_addr *ipv6 = buf;
1010 
1011                 if (size < sizeof (struct in6_addr))
1012                         return (EOVERFLOW);
1013 
1014                 if (IN6_IS_ADDR_V4COMPAT(ipv6))
1015                         return (EINVAL);
1016 
1017                 if (IN6_IS_ADDR_MULTICAST(ipv6))
1018                         return (EINVAL);
1019 
1020                 if (IN6_IS_ADDR_6TO4(ipv6))
1021                         return (EINVAL);
1022 
1023                 if (IN6_IS_ADDR_V4MAPPED(ipv6)) {
1024                         ipaddr_t v4;
1025                         IN6_V4MAPPED_TO_IPADDR(ipv6, v4);
1026                         if (IN_MULTICAST(v4))
1027                                 return (EINVAL);
1028                 }
1029 
1030                 mutex_enter(&svp->svp_lock);
1031                 bcopy(buf, &svp->svp_uip, sizeof (struct in6_addr));
1032                 svp->svp_huip = B_TRUE;
1033                 mutex_exit(&svp->svp_lock);
1034                 return (0);
1035         }
1036 
1037         /* svp/underlay_port */
1038         if (strcmp(pname, varpd_svp_props[3]) == 0) {
1039                 const uint64_t *valp = buf;
1040                 if (size < sizeof (uint64_t))
1041                         return (EOVERFLOW);
1042 
1043                 if (*valp == 0 || *valp > UINT16_MAX)
1044                         return (EINVAL);
1045 
1046                 mutex_enter(&svp->svp_lock);
1047                 svp->svp_uport = (uint16_t)*valp;
1048                 mutex_exit(&svp->svp_lock);
1049 
1050                 return (0);
1051         }
1052 
1053         /* svp/dcid */
1054         if (strcmp(pname, varpd_svp_props[4]) == 0) {
1055                 const uint64_t *valp = buf;
1056                 if (size < sizeof (uint64_t))
1057                         return (EOVERFLOW);
1058 
1059                 if (*valp == 0 || *valp > UINT32_MAX - 1)
1060                         return (EINVAL);
1061 
1062                 mutex_enter(&svp->svp_lock);
1063                 svp->svp_dcid = (uint32_t)*valp;
1064                 mutex_exit(&svp->svp_lock);
1065 
1066                 return (0);
1067         }
1068 
1069         /* svp/router_oui */
1070         if (strcmp(pname, varpd_svp_props[5]) == 0) {
1071                 if (size < ETHERADDRL)
1072                         return (EOVERFLOW);
1073                 mutex_enter(&svp->svp_lock);
1074                 bcopy(buf, &svp->svp_router_oui, ETHERADDRL);
1075                 /* Zero-out the low three bytes. */
1076                 svp->svp_router_oui[3] = 0;
1077                 svp->svp_router_oui[4] = 0;
1078                 svp->svp_router_oui[5] = 0;
1079                 mutex_exit(&svp->svp_lock);
1080                 return (0);
1081         }
1082 
1083         return (EINVAL);
1084 }
1085 
1086 static int
1087 varpd_svp_save(void *arg, nvlist_t *nvp)
1088 {
1089         int ret;
1090         svp_t *svp = arg;
1091 
1092         mutex_enter(&svp->svp_lock);
1093         /* svp/host */
1094         if (svp->svp_host != NULL) {
1095                 if ((ret = nvlist_add_string(nvp, varpd_svp_props[0],
1096                     svp->svp_host)) != 0) {
1097                         mutex_exit(&svp->svp_lock);
1098                         return (ret);
1099                 }
1100         }
1101 
1102         /* svp/port */
1103         if (svp->svp_port != 0) {
1104                 if ((ret = nvlist_add_uint16(nvp, varpd_svp_props[1],
1105                     svp->svp_port)) != 0) {
1106                         mutex_exit(&svp->svp_lock);
1107                         return (ret);
1108                 }
1109         }
1110 
1111         /* svp/underlay_ip */
1112         if (svp->svp_huip == B_TRUE) {
1113                 char buf[INET6_ADDRSTRLEN];
1114 
1115                 if (inet_ntop(AF_INET6, &svp->svp_uip, buf, sizeof (buf)) ==
1116                     NULL)
1117                         libvarpd_panic("unexpected inet_ntop failure: %d",
1118                             errno);
1119 
1120                 if ((ret = nvlist_add_string(nvp, varpd_svp_props[2],
1121                     buf)) != 0) {
1122                         mutex_exit(&svp->svp_lock);
1123                         return (ret);
1124                 }
1125         }
1126 
1127         /* svp/underlay_port */
1128         if (svp->svp_uport != 0) {
1129                 if ((ret = nvlist_add_uint16(nvp, varpd_svp_props[3],
1130                     svp->svp_uport)) != 0) {
1131                         mutex_exit(&svp->svp_lock);
1132                         return (ret);
1133                 }
1134         }
1135 
1136         /* svp/dcid */
1137         if (svp->svp_dcid != 0) {
1138                 if ((ret = nvlist_add_uint32(nvp, varpd_svp_props[4],
1139                     svp->svp_dcid)) != 0) {
1140                         mutex_exit(&svp->svp_lock);
1141                         return (ret);
1142                 }
1143         }
1144 
1145         /* svp/router_oui */
1146         if (!ether_is_zero(&svp->svp_router_oui)) {
1147                 char buf[ETHERADDRSTRL];
1148 
1149                 if (ether_ntoa_r((struct ether_addr *)&svp->svp_router_oui,
1150                     buf) == NULL) {
1151                         libvarpd_panic("unexpected ether_ntoa_r failure: %d",
1152                             errno);
1153                 }
1154 
1155                 if ((ret = nvlist_add_string(nvp, varpd_svp_props[5],
1156                     buf)) != 0) {
1157                         mutex_exit(&svp->svp_lock);
1158                         return (ret);
1159                 }
1160         }
1161 
1162         mutex_exit(&svp->svp_lock);
1163         return (0);
1164 }
1165 
1166 static int
1167 varpd_svp_restore(nvlist_t *nvp, varpd_provider_handle_t *hdl,
1168     overlay_plugin_dest_t dest, void **outp)
1169 {
1170         int ret;
1171         svp_t *svp;
1172         char *ipstr, *hstr, *etherstr;
1173 
1174         if (varpd_svp_valid_dest(dest) == B_FALSE)
1175                 return (ENOTSUP);
1176 
1177         if ((ret = varpd_svp_create(hdl, (void **)&svp, dest)) != 0)
1178                 return (ret);
1179 
1180         /* svp/host */
1181         if ((ret = nvlist_lookup_string(nvp, varpd_svp_props[0],
1182             &hstr)) != 0) {
1183                 if (ret != ENOENT) {
1184                         varpd_svp_destroy(svp);
1185                         return (ret);
1186                 }
1187                 svp->svp_host = NULL;
1188         } else {
1189                 size_t blen = strlen(hstr) + 1;
1190                 svp->svp_host = umem_alloc(blen, UMEM_DEFAULT);
1191                 (void) strlcpy(svp->svp_host, hstr, blen);
1192         }
1193 
1194         /* svp/port */
1195         if ((ret = nvlist_lookup_uint16(nvp, varpd_svp_props[1],
1196             &svp->svp_port)) != 0) {
1197                 if (ret != ENOENT) {
1198                         varpd_svp_destroy(svp);
1199                         return (ret);
1200                 }
1201                 svp->svp_port = 0;
1202         }
1203 
1204         /* svp/underlay_ip */
1205         if ((ret = nvlist_lookup_string(nvp, varpd_svp_props[2],
1206             &ipstr)) != 0) {
1207                 if (ret != ENOENT) {
1208                         varpd_svp_destroy(svp);
1209                         return (ret);
1210                 }
1211                 svp->svp_huip = B_FALSE;
1212         } else {
1213                 ret = inet_pton(AF_INET6, ipstr, &svp->svp_uip);
1214                 if (ret == -1) {
1215                         assert(errno == EAFNOSUPPORT);
1216                         libvarpd_panic("unexpected inet_pton failure: %d",
1217                             errno);
1218                 }
1219 
1220                 if (ret == 0) {
1221                         varpd_svp_destroy(svp);
1222                         return (EINVAL);
1223                 }
1224                 svp->svp_huip = B_TRUE;
1225         }
1226 
1227         /* svp/underlay_port */
1228         if ((ret = nvlist_lookup_uint16(nvp, varpd_svp_props[3],
1229             &svp->svp_uport)) != 0) {
1230                 if (ret != ENOENT) {
1231                         varpd_svp_destroy(svp);
1232                         return (ret);
1233                 }
1234                 svp->svp_uport = 0;
1235         }
1236 
1237         /* svp/dcid */
1238         if ((ret = nvlist_lookup_uint32(nvp, varpd_svp_props[4],
1239             &svp->svp_dcid)) != 0) {
1240                 if (ret != ENOENT) {
1241                         varpd_svp_destroy(svp);
1242                         return (ret);
1243                 }
1244                 svp->svp_dcid = 0;
1245         }
1246 
1247         /* svp/router_oui */
1248         if ((ret = nvlist_lookup_string(nvp, varpd_svp_props[5],
1249             &etherstr)) != 0) {
1250                 if (ret != ENOENT) {
1251                         varpd_svp_destroy(svp);
1252                         return (ret);
1253                 }
1254                 bzero(&svp->svp_router_oui, ETHERADDRL);
1255         } else if (ether_aton_r(etherstr,
1256             (struct ether_addr *)&svp->svp_router_oui) == NULL) {
1257                 libvarpd_panic("unexpected ether_aton_r failure: %d", errno);
1258         }
1259 
1260         svp->svp_hdl = hdl;
1261         *outp = svp;
1262         return (0);
1263 }
1264 
1265 static void
1266 varpd_svp_arp(void *arg, varpd_arp_handle_t *vah, int type,
1267     const struct sockaddr *sock, uint16_t vlan __unused, uint8_t *out)
1268 {
1269         svp_t *svp = arg;
1270         svp_lookup_t *svl;
1271 
1272         if (type != VARPD_QTYPE_ETHERNET) {
1273                 libvarpd_plugin_arp_reply(vah, VARPD_LOOKUP_DROP);
1274                 return;
1275         }
1276 
1277         svl = umem_cache_alloc(svp_lookup_cache, UMEM_DEFAULT);
1278         if (svl == NULL) {
1279                 libvarpd_plugin_arp_reply(vah, VARPD_LOOKUP_DROP);
1280                 return;
1281         }
1282 
1283         svl->svl_type = SVP_L_VL3;
1284         svl->svl_u.svl_vl3.svl_vah = vah;
1285         svl->svl_u.svl_vl3.svl_out = out;
1286         svp_remote_vl3_lookup(svp, &svl->svl_query, sock, svl);
1287 }
1288 
1289 static const varpd_plugin_ops_t varpd_svp_ops = {
1290         0,
1291         varpd_svp_create,
1292         varpd_svp_start,
1293         varpd_svp_stop,
1294         varpd_svp_destroy,
1295         NULL,
1296         varpd_svp_lookup,
1297         varpd_svp_nprops,
1298         varpd_svp_propinfo,
1299         varpd_svp_getprop,
1300         varpd_svp_setprop,
1301         varpd_svp_save,
1302         varpd_svp_restore,
1303         varpd_svp_arp,
1304         NULL
1305 };
1306 
1307 static int
1308 svp_bunyan_init(void)
1309 {
1310         int ret;
1311 
1312         if ((ret = bunyan_init("svp", &svp_bunyan)) != 0)
1313                 return (ret);
1314         ret = bunyan_stream_add(svp_bunyan, "stderr", BUNYAN_L_INFO,
1315             bunyan_stream_fd, (void *)STDERR_FILENO);
1316         if (ret != 0)
1317                 bunyan_fini(svp_bunyan);
1318         return (ret);
1319 }
1320 
1321 static void
1322 svp_bunyan_fini(void)
1323 {
1324         if (svp_bunyan != NULL)
1325                 bunyan_fini(svp_bunyan);
1326 }
1327 
1328 #pragma init(varpd_svp_init)
1329 static void
1330 varpd_svp_init(void)
1331 {
1332         int err;
1333         varpd_plugin_register_t *vpr;
1334 
1335         if (svp_bunyan_init() != 0)
1336                 return;
1337 
1338         if ((err = svp_host_init()) != 0) {
1339                 (void) bunyan_error(svp_bunyan, "failed to init host subsystem",
1340                     BUNYAN_T_INT32, "error", err,
1341                     BUNYAN_T_END);
1342                 svp_bunyan_fini();
1343                 return;
1344         }
1345 
1346         svp_lookup_cache = umem_cache_create("svp_lookup",
1347             sizeof (svp_lookup_t),  0, NULL, NULL, NULL, NULL, NULL, 0);
1348         if (svp_lookup_cache == NULL) {
1349                 (void) bunyan_error(svp_bunyan,
1350                     "failed to create svp_lookup cache",
1351                     BUNYAN_T_INT32, "error", errno,
1352                     BUNYAN_T_END);
1353                 svp_bunyan_fini();
1354                 return;
1355         }
1356 
1357         if ((err = svp_event_init()) != 0) {
1358                 (void) bunyan_error(svp_bunyan,
1359                     "failed to init event subsystem",
1360                     BUNYAN_T_INT32, "error", err,
1361                     BUNYAN_T_END);
1362                 svp_bunyan_fini();
1363                 umem_cache_destroy(svp_lookup_cache);
1364                 return;
1365         }
1366 
1367         if ((err = svp_timer_init()) != 0) {
1368                 (void) bunyan_error(svp_bunyan,
1369                     "failed to init timer subsystem",
1370                     BUNYAN_T_INT32, "error", err,
1371                     BUNYAN_T_END);
1372                 svp_event_fini();
1373                 umem_cache_destroy(svp_lookup_cache);
1374                 svp_bunyan_fini();
1375                 return;
1376         }
1377 
1378         if ((err = svp_remote_init()) != 0) {
1379                 (void) bunyan_error(svp_bunyan,
1380                     "failed to init remote subsystem",
1381                     BUNYAN_T_INT32, "error", err,
1382                     BUNYAN_T_END);
1383                 svp_event_fini();
1384                 umem_cache_destroy(svp_lookup_cache);
1385                 svp_bunyan_fini();
1386                 return;
1387         }
1388 
1389         vpr = libvarpd_plugin_alloc(VARPD_CURRENT_VERSION, &err);
1390         if (vpr == NULL) {
1391                 (void) bunyan_error(svp_bunyan,
1392                     "failed to alloc varpd plugin",
1393                     BUNYAN_T_INT32, "error", err,
1394                     BUNYAN_T_END);
1395                 svp_remote_fini();
1396                 svp_event_fini();
1397                 umem_cache_destroy(svp_lookup_cache);
1398                 svp_bunyan_fini();
1399                 return;
1400         }
1401 
1402         vpr->vpr_mode = OVERLAY_TARGET_DYNAMIC;
1403         vpr->vpr_name = "svp";
1404         vpr->vpr_ops = &varpd_svp_ops;
1405 
1406         if ((err = libvarpd_plugin_register(vpr)) != 0) {
1407                 (void) bunyan_error(svp_bunyan,
1408                     "failed to register varpd plugin",
1409                     BUNYAN_T_INT32, "error", err,
1410                     BUNYAN_T_END);
1411                 svp_remote_fini();
1412                 svp_event_fini();
1413                 umem_cache_destroy(svp_lookup_cache);
1414                 svp_bunyan_fini();
1415 
1416         }
1417         libvarpd_plugin_free(vpr);
1418 }