1.\" Copyright (c) 2001-2003 International Computer Science Institute 2.\" 3.\" Permission is hereby granted, free of charge, to any person obtaining a 4.\" copy of this software and associated documentation files (the "Software"), 5.\" to deal in the Software without restriction, including without limitation 6.\" the rights to use, copy, modify, merge, publish, distribute, sublicense, 7.\" and/or sell copies of the Software, and to permit persons to whom the 8.\" Software is furnished to do so, subject to the following conditions: 9.\" 10.\" The above copyright notice and this permission notice shall be included in 11.\" all copies or substantial portions of the Software. 12.\" 13.\" The names and trademarks of copyright holders may not be used in 14.\" advertising or publicity pertaining to the software without specific 15.\" prior permission. Title to copyright in this software and any associated 16.\" documentation will at all times remain with the copyright holders. 17.\" 18.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19.\" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20.\" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21.\" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22.\" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23.\" FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24.\" DEALINGS IN THE SOFTWARE. 25.\" 26.\" $FreeBSD$ 27.\" 28.Dd September 4, 2003 29.Dt MULTICAST 4 30.Os 31.\" 32.Sh NAME 33.Nm multicast 34.Nd Multicast Routing 35.\" 36.Sh SYNOPSIS 37.Cd "options MROUTING" 38.Pp 39.In sys/types.h 40.In sys/socket.h 41.In netinet/in.h 42.In netinet/ip_mroute.h 43.In netinet6/ip6_mroute.h 44.Ft int 45.Fn getsockopt "int s" IPPROTO_IP MRT_INIT "void *optval" "socklen_t *optlen" 46.Ft int 47.Fn setsockopt "int s" IPPROTO_IP MRT_INIT "const void *optval" "socklen_t optlen" 48.Ft int 49.Fn getsockopt "int s" IPPROTO_IPV6 MRT6_INIT "void *optval" "socklen_t *optlen" 50.Ft int 51.Fn setsockopt "int s" IPPROTO_IPV6 MRT6_INIT "const void *optval" "socklen_t optlen" 52.Sh DESCRIPTION 53.Tn "Multicast routing" 54is used to efficiently propagate data 55packets to a set of multicast listeners in multipoint networks. 56If unicast is used to replicate the data to all listeners, 57then some of the network links may carry multiple copies of the same 58data packets. 59With multicast routing, the overhead is reduced to one copy 60(at most) per network link. 61.Pp 62All multicast-capable routers must run a common multicast routing 63protocol. 64The Distance Vector Multicast Routing Protocol (DVMRP) 65was the first developed multicast routing protocol. 66Later, other protocols such as Multicast Extensions to OSPF (MOSPF), 67Core Based Trees (CBT), 68Protocol Independent Multicast - Sparse Mode (PIM-SM), 69and Protocol Independent Multicast - Dense Mode (PIM-DM) 70were developed as well. 71.Pp 72To start multicast routing, 73the user must enable multicast forwarding in the kernel 74(see 75.Sx SYNOPSIS 76about the kernel configuration options), 77and must run a multicast routing capable user-level process. 78From developer's point of view, 79the programming guide described in the 80.Sx "Programming Guide" 81section should be used to control the multicast forwarding in the kernel. 82.\" 83.Ss Programming Guide 84This section provides information about the basic multicast routing API. 85The so-called 86.Dq advanced multicast API 87is described in the 88.Sx "Advanced Multicast API Programming Guide" 89section. 90.Pp 91First, a multicast routing socket must be open. 92That socket would be used 93to control the multicast forwarding in the kernel. 94Note that most operations below require certain privilege 95(i.e., root privilege): 96.Pp 97.Bd -literal 98/* IPv4 */ 99int mrouter_s4; 100mrouter_s4 = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP); 101.Ed 102.Pp 103.Bd -literal 104int mrouter_s6; 105mrouter_s6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); 106.Ed 107.Pp 108Note that if the router needs to open an IGMP or ICMPv6 socket 109(in case of IPv4 and IPv6 respectively) 110for sending or receiving of IGMP or MLD multicast group membership messages, 111then the same mrouter_s4 or mrouter_s6 sockets should be used 112for sending and receiving respectively IGMP or MLD messages. 113In case of BSD-derived kernel, it may be possible to open separate sockets 114for IGMP or MLD messages only. 115However, some other kernels (e.g., Linux) require that the multicast 116routing socket must be used for sending and receiving of IGMP or MLD 117messages. 118Therefore, for portability reason the multicast 119routing socket should be reused for IGMP and MLD messages as well. 120.Pp 121After the multicast routing socket is open, it can be used to enable 122or disable multicast forwarding in the kernel: 123.Bd -literal 124/* IPv4 */ 125int v = 1; /* 1 to enable, or 0 to disable */ 126setsockopt(mrouter_s4, IPPROTO_IP, MRT_INIT, (void *)&v, sizeof(v)); 127.Ed 128.Pp 129.Bd -literal 130/* IPv6 */ 131int v = 1; /* 1 to enable, or 0 to disable */ 132setsockopt(mrouter_s6, IPPROTO_IPV6, MRT6_INIT, (void *)&v, sizeof(v)); 133\&... 134/* If necessary, filter all ICMPv6 messages */ 135struct icmp6_filter filter; 136ICMP6_FILTER_SETBLOCKALL(&filter); 137setsockopt(mrouter_s6, IPPROTO_ICMPV6, ICMP6_FILTER, (void *)&filter, 138 sizeof(filter)); 139.Ed 140.Pp 141After multicast forwarding is enabled, the multicast routing socket 142can be used to enable PIM processing in the kernel if we are running PIM-SM or 143PIM-DM 144(see 145.Xr pim 4 ) . 146.Pp 147For each network interface (e.g., physical or a virtual tunnel) 148that would be used for multicast forwarding, a corresponding 149multicast interface must be added to the kernel: 150.Bd -literal 151/* IPv4 */ 152struct vifctl vc; 153memset(&vc, 0, sizeof(vc)); 154/* Assign all vifctl fields as appropriate */ 155vc.vifc_vifi = vif_index; 156vc.vifc_flags = vif_flags; 157vc.vifc_threshold = min_ttl_threshold; 158vc.vifc_rate_limit = max_rate_limit; 159memcpy(&vc.vifc_lcl_addr, &vif_local_address, sizeof(vc.vifc_lcl_addr)); 160if (vc.vifc_flags & VIFF_TUNNEL) 161 memcpy(&vc.vifc_rmt_addr, &vif_remote_address, 162 sizeof(vc.vifc_rmt_addr)); 163setsockopt(mrouter_s4, IPPROTO_IP, MRT_ADD_VIF, (void *)&vc, 164 sizeof(vc)); 165.Ed 166.Pp 167The 168.Dq vif_index 169must be unique per vif. 170The 171.Dq vif_flags 172contains the 173.Dq VIFF_* 174flags as defined in <netinet/ip_mroute.h>. 175The 176.Dq min_ttl_threshold 177contains the minimum TTL a multicast data packet must have to be 178forwarded on that vif. 179Typically, it would have value of 1. 180The 181.Dq max_rate_limit 182contains the maximum rate (in bits/s) of the multicast data packets forwarded 183on that vif. 184Value of 0 means no limit. 185The 186.Dq vif_local_address 187contains the local IP address of the corresponding local interface. 188The 189.Dq vif_remote_address 190contains the remote IP address in case of DVMRP multicast tunnels. 191.Pp 192.Bd -literal 193/* IPv6 */ 194struct mif6ctl mc; 195memset(&mc, 0, sizeof(mc)); 196/* Assign all mif6ctl fields as appropriate */ 197mc.mif6c_mifi = mif_index; 198mc.mif6c_flags = mif_flags; 199mc.mif6c_pifi = pif_index; 200setsockopt(mrouter_s6, IPPROTO_IPV6, MRT6_ADD_MIF, (void *)&mc, 201 sizeof(mc)); 202.Ed 203.Pp 204The 205.Dq mif_index 206must be unique per vif. 207The 208.Dq mif_flags 209contains the 210.Dq MIFF_* 211flags as defined in <netinet6/ip6_mroute.h>. 212The 213.Dq pif_index 214is the physical interface index of the corresponding local interface. 215.Pp 216A multicast interface is deleted by: 217.Bd -literal 218/* IPv4 */ 219vifi_t vifi = vif_index; 220setsockopt(mrouter_s4, IPPROTO_IP, MRT_DEL_VIF, (void *)&vifi, 221 sizeof(vifi)); 222.Ed 223.Pp 224.Bd -literal 225/* IPv6 */ 226mifi_t mifi = mif_index; 227setsockopt(mrouter_s6, IPPROTO_IPV6, MRT6_DEL_MIF, (void *)&mifi, 228 sizeof(mifi)); 229.Ed 230.Pp 231After the multicast forwarding is enabled, and the multicast virtual 232interfaces are 233added, the kernel may deliver upcall messages (also called signals 234later in this text) on the multicast routing socket that was open 235earlier with 236.Dq MRT_INIT 237or 238.Dq MRT6_INIT . 239The IPv4 upcalls have 240.Dq struct igmpmsg 241header (see <netinet/ip_mroute.h>) with field 242.Dq im_mbz 243set to zero. 244Note that this header follows the structure of 245.Dq struct ip 246with the protocol field 247.Dq ip_p 248set to zero. 249The IPv6 upcalls have 250.Dq struct mrt6msg 251header (see <netinet6/ip6_mroute.h>) with field 252.Dq im6_mbz 253set to zero. 254Note that this header follows the structure of 255.Dq struct ip6_hdr 256with the next header field 257.Dq ip6_nxt 258set to zero. 259.Pp 260The upcall header contains field 261.Dq im_msgtype 262and 263.Dq im6_msgtype 264with the type of the upcall 265.Dq IGMPMSG_* 266and 267.Dq MRT6MSG_* 268for IPv4 and IPv6 respectively. 269The values of the rest of the upcall header fields 270and the body of the upcall message depend on the particular upcall type. 271.Pp 272If the upcall message type is 273.Dq IGMPMSG_NOCACHE 274or 275.Dq MRT6MSG_NOCACHE , 276this is an indication that a multicast packet has reached the multicast 277router, but the router has no forwarding state for that packet. 278Typically, the upcall would be a signal for the multicast routing 279user-level process to install the appropriate Multicast Forwarding 280Cache (MFC) entry in the kernel. 281.Pp 282A MFC entry is added by: 283.Bd -literal 284/* IPv4 */ 285struct mfcctl mc; 286memset(&mc, 0, sizeof(mc)); 287memcpy(&mc.mfcc_origin, &source_addr, sizeof(mc.mfcc_origin)); 288memcpy(&mc.mfcc_mcastgrp, &group_addr, sizeof(mc.mfcc_mcastgrp)); 289mc.mfcc_parent = iif_index; 290for (i = 0; i < maxvifs; i++) 291 mc.mfcc_ttls[i] = oifs_ttl[i]; 292setsockopt(mrouter_s4, IPPROTO_IP, MRT_ADD_MFC, 293 (void *)&mc, sizeof(mc)); 294.Ed 295.Pp 296.Bd -literal 297/* IPv6 */ 298struct mf6cctl mc; 299memset(&mc, 0, sizeof(mc)); 300memcpy(&mc.mf6cc_origin, &source_addr, sizeof(mc.mf6cc_origin)); 301memcpy(&mc.mf6cc_mcastgrp, &group_addr, sizeof(mf6cc_mcastgrp)); 302mc.mf6cc_parent = iif_index; 303for (i = 0; i < maxvifs; i++) 304 if (oifs_ttl[i] > 0) 305 IF_SET(i, &mc.mf6cc_ifset); 306setsockopt(mrouter_s4, IPPROTO_IPV6, MRT6_ADD_MFC, 307 (void *)&mc, sizeof(mc)); 308.Ed 309.Pp 310The 311.Dq source_addr 312and 313.Dq group_addr 314are the source and group address of the multicast packet (as set 315in the upcall message). 316The 317.Dq iif_index 318is the virtual interface index of the multicast interface the multicast 319packets for this specific source and group address should be received on. 320The 321.Dq oifs_ttl[] 322array contains the minimum TTL (per interface) a multicast packet 323should have to be forwarded on an outgoing interface. 324If the TTL value is zero, the corresponding interface is not included 325in the set of outgoing interfaces. 326Note that in case of IPv6 only the set of outgoing interfaces can 327be specified. 328.Pp 329A MFC entry is deleted by: 330.Bd -literal 331/* IPv4 */ 332struct mfcctl mc; 333memset(&mc, 0, sizeof(mc)); 334memcpy(&mc.mfcc_origin, &source_addr, sizeof(mc.mfcc_origin)); 335memcpy(&mc.mfcc_mcastgrp, &group_addr, sizeof(mc.mfcc_mcastgrp)); 336setsockopt(mrouter_s4, IPPROTO_IP, MRT_DEL_MFC, 337 (void *)&mc, sizeof(mc)); 338.Ed 339.Pp 340.Bd -literal 341/* IPv6 */ 342struct mf6cctl mc; 343memset(&mc, 0, sizeof(mc)); 344memcpy(&mc.mf6cc_origin, &source_addr, sizeof(mc.mf6cc_origin)); 345memcpy(&mc.mf6cc_mcastgrp, &group_addr, sizeof(mf6cc_mcastgrp)); 346setsockopt(mrouter_s4, IPPROTO_IPV6, MRT6_DEL_MFC, 347 (void *)&mc, sizeof(mc)); 348.Ed 349.Pp 350The following method can be used to get various statistics per 351installed MFC entry in the kernel (e.g., the number of forwarded 352packets per source and group address): 353.Bd -literal 354/* IPv4 */ 355struct sioc_sg_req sgreq; 356memset(&sgreq, 0, sizeof(sgreq)); 357memcpy(&sgreq.src, &source_addr, sizeof(sgreq.src)); 358memcpy(&sgreq.grp, &group_addr, sizeof(sgreq.grp)); 359ioctl(mrouter_s4, SIOCGETSGCNT, &sgreq); 360.Ed 361.Pp 362.Bd -literal 363/* IPv6 */ 364struct sioc_sg_req6 sgreq; 365memset(&sgreq, 0, sizeof(sgreq)); 366memcpy(&sgreq.src, &source_addr, sizeof(sgreq.src)); 367memcpy(&sgreq.grp, &group_addr, sizeof(sgreq.grp)); 368ioctl(mrouter_s6, SIOCGETSGCNT_IN6, &sgreq); 369.Ed 370.Pp 371The following method can be used to get various statistics per 372multicast virtual interface in the kernel (e.g., the number of forwarded 373packets per interface): 374.Bd -literal 375/* IPv4 */ 376struct sioc_vif_req vreq; 377memset(&vreq, 0, sizeof(vreq)); 378vreq.vifi = vif_index; 379ioctl(mrouter_s4, SIOCGETVIFCNT, &vreq); 380.Ed 381.Pp 382.Bd -literal 383/* IPv6 */ 384struct sioc_mif_req6 mreq; 385memset(&mreq, 0, sizeof(mreq)); 386mreq.mifi = vif_index; 387ioctl(mrouter_s6, SIOCGETMIFCNT_IN6, &mreq); 388.Ed 389.Pp 390.Ss Advanced Multicast API Programming Guide 391If we want to add new features in the kernel, it becomes difficult 392to preserve backward compatibility (binary and API), 393and at the same time to allow user-level processes to take advantage of 394the new features (if the kernel supports them). 395.Pp 396One of the mechanisms that allows us to preserve the backward 397compatibility is a sort of negotiation 398between the user-level process and the kernel: 399.Bl -enum 400.It 401The user-level process tries to enable in the kernel the set of new 402features (and the corresponding API) it would like to use. 403.It 404The kernel returns the (sub)set of features it knows about 405and is willing to be enabled. 406.It 407The user-level process uses only that set of features 408the kernel has agreed on. 409.El 410.\" 411.Pp 412To support backward compatibility, if the user-level process doesn't 413ask for any new features, the kernel defaults to the basic 414multicast API (see the 415.Sx "Programming Guide" 416section). 417.\" XXX: edit as appropriate after the advanced multicast API is 418.\" supported under IPv6 419Currently, the advanced multicast API exists only for IPv4; 420in the future there will be IPv6 support as well. 421.Pp 422Below is a summary of the expandable API solution. 423Note that all new options and structures are defined 424in <netinet/ip_mroute.h> and <netinet6/ip6_mroute.h>, 425unless stated otherwise. 426.Pp 427The user-level process uses new get/setsockopt() options to 428perform the API features negotiation with the kernel. 429This negotiation must be performed right after the multicast routing 430socket is open. 431The set of desired/allowed features is stored in a bitset 432(currently, in uint32_t; i.e., maximum of 32 new features). 433The new get/setsockopt() options are 434.Dq MRT_API_SUPPORT 435and 436.Dq MRT_API_CONFIG . 437Example: 438.Bd -literal 439uint32_t v; 440getsockopt(sock, IPPROTO_IP, MRT_API_SUPPORT, (void *)&v, sizeof(v)); 441.Ed 442.Pp 443would set in 444.Dq v 445the pre-defined bits that the kernel API supports. 446The eight least significant bits in uint32_t are same as the 447eight possible flags 448.Dq MRT_MFC_FLAGS_* 449that can be used in 450.Dq mfcc_flags 451as part of the new definition of 452.Dq struct mfcctl 453(see below about those flags), which leaves 24 flags for other new features. 454The value returned by getsockopt(MRT_API_SUPPORT) is read-only; in other 455words, setsockopt(MRT_API_SUPPORT) would fail. 456.Pp 457To modify the API, and to set some specific feature in the kernel, then: 458.Bd -literal 459uint32_t v = MRT_MFC_FLAGS_DISABLE_WRONGVIF; 460if (setsockopt(sock, IPPROTO_IP, MRT_API_CONFIG, (void *)&v, sizeof(v)) 461 != 0) { 462 return (ERROR); 463} 464if (v & MRT_MFC_FLAGS_DISABLE_WRONGVIF) 465 return (OK); /* Success */ 466else 467 return (ERROR); 468.Ed 469.Pp 470In other words, when setsockopt(MRT_API_CONFIG) is called, the 471argument to it specifies the desired set of features to 472be enabled in the API and the kernel. 473The return value in 474.Dq v 475is the actual (sub)set of features that were enabled in the kernel. 476To obtain later the same set of features that were enabled, then: 477.Bd -literal 478getsockopt(sock, IPPROTO_IP, MRT_API_CONFIG, (void *)&v, sizeof(v)); 479.Ed 480.Pp 481The set of enabled features is global. 482In other words, setsockopt(MRT_API_CONFIG) 483should be called right after setsockopt(MRT_INIT). 484.Pp 485Currently, the following set of new features is defined: 486.Bd -literal 487#define MRT_MFC_FLAGS_DISABLE_WRONGVIF (1 << 0) /* disable WRONGVIF signals */ 488#define MRT_MFC_FLAGS_BORDER_VIF (1 << 1) /* border vif */ 489#define MRT_MFC_RP (1 << 8) /* enable RP address */ 490#define MRT_MFC_BW_UPCALL (1 << 9) /* enable bw upcalls */ 491.Ed 492.\" .Pp 493.\" In the future there might be: 494.\" .Bd -literal 495.\" #define MRT_MFC_GROUP_SPECIFIC (1 << 10) /* allow (*,G) MFC entries */ 496.\" .Ed 497.\" .Pp 498.\" to allow (*,G) MFC entries (i.e., group-specific entries) in the kernel. 499.\" For now this is left-out until it is clear whether 500.\" (*,G) MFC support is the preferred solution instead of something more generic 501.\" solution for example. 502.\" 503.\" 2. The newly defined struct mfcctl2. 504.\" 505.Pp 506The advanced multicast API uses a newly defined 507.Dq struct mfcctl2 508instead of the traditional 509.Dq struct mfcctl . 510The original 511.Dq struct mfcctl 512is kept as is. 513The new 514.Dq struct mfcctl2 515is: 516.Bd -literal 517/* 518 * The new argument structure for MRT_ADD_MFC and MRT_DEL_MFC overlays 519 * and extends the old struct mfcctl. 520 */ 521struct mfcctl2 { 522 /* the mfcctl fields */ 523 struct in_addr mfcc_origin; /* ip origin of mcasts */ 524 struct in_addr mfcc_mcastgrp; /* multicast group associated*/ 525 vifi_t mfcc_parent; /* incoming vif */ 526 u_char mfcc_ttls[MAXVIFS];/* forwarding ttls on vifs */ 527 528 /* extension fields */ 529 uint8_t mfcc_flags[MAXVIFS];/* the MRT_MFC_FLAGS_* flags*/ 530 struct in_addr mfcc_rp; /* the RP address */ 531}; 532.Ed 533.Pp 534The new fields are 535.Dq mfcc_flags[MAXVIFS] 536and 537.Dq mfcc_rp . 538Note that for compatibility reasons they are added at the end. 539.Pp 540The 541.Dq mfcc_flags[MAXVIFS] 542field is used to set various flags per 543interface per (S,G) entry. 544Currently, the defined flags are: 545.Bd -literal 546#define MRT_MFC_FLAGS_DISABLE_WRONGVIF (1 << 0) /* disable WRONGVIF signals */ 547#define MRT_MFC_FLAGS_BORDER_VIF (1 << 1) /* border vif */ 548.Ed 549.Pp 550The 551.Dq MRT_MFC_FLAGS_DISABLE_WRONGVIF 552flag is used to explicitly disable the 553.Dq IGMPMSG_WRONGVIF 554kernel signal at the (S,G) granularity if a multicast data packet 555arrives on the wrong interface. 556Usually, this signal is used to 557complete the shortest-path switch in case of PIM-SM multicast routing, 558or to trigger a PIM assert message. 559However, it should not be delivered for interfaces that are not in 560the outgoing interface set, and that are not expecting to 561become an incoming interface. 562Hence, if the 563.Dq MRT_MFC_FLAGS_DISABLE_WRONGVIF 564flag is set for some of the 565interfaces, then a data packet that arrives on that interface for 566that MFC entry will NOT trigger a WRONGVIF signal. 567If that flag is not set, then a signal is triggered (the default action). 568.Pp 569The 570.Dq MRT_MFC_FLAGS_BORDER_VIF 571flag is used to specify whether the Border-bit in PIM 572Register messages should be set (in case when the Register encapsulation 573is performed inside the kernel). 574If it is set for the special PIM Register kernel virtual interface 575(see 576.Xr pim 4 ) , 577the Border-bit in the Register messages sent to the RP will be set. 578.Pp 579The remaining six bits are reserved for future usage. 580.Pp 581The 582.Dq mfcc_rp 583field is used to specify the RP address (in case of PIM-SM multicast routing) 584for a multicast 585group G if we want to perform kernel-level PIM Register encapsulation. 586The 587.Dq mfcc_rp 588field is used only if the 589.Dq MRT_MFC_RP 590advanced API flag/capability has been successfully set by 591setsockopt(MRT_API_CONFIG). 592.Pp 593.\" 594.\" 3. Kernel-level PIM Register encapsulation 595.\" 596If the 597.Dq MRT_MFC_RP 598flag was successfully set by 599setsockopt(MRT_API_CONFIG), then the kernel will attempt to perform 600the PIM Register encapsulation itself instead of sending the 601multicast data packets to user level (inside IGMPMSG_WHOLEPKT 602upcalls) for user-level encapsulation. 603The RP address would be taken from the 604.Dq mfcc_rp 605field 606inside the new 607.Dq struct mfcctl2 . 608However, even if the 609.Dq MRT_MFC_RP 610flag was successfully set, if the 611.Dq mfcc_rp 612field was set to 613.Dq INADDR_ANY , 614then the 615kernel will still deliver an IGMPMSG_WHOLEPKT upcall with the 616multicast data packet to the user-level process. 617.Pp 618In addition, if the multicast data packet is too large to fit within 619a single IP packet after the PIM Register encapsulation (e.g., if 620its size was on the order of 65500 bytes), the data packet will be 621fragmented, and then each of the fragments will be encapsulated 622separately. 623Note that typically a multicast data packet can be that 624large only if it was originated locally from the same hosts that 625performs the encapsulation; otherwise the transmission of the 626multicast data packet over Ethernet for example would have 627fragmented it into much smaller pieces. 628.\" 629.\" Note that if this code is ported to IPv6, we may need the kernel to 630.\" perform MTU discovery to the RP, and keep those discoveries inside 631.\" the kernel so the encapsulating router may send back ICMP 632.\" Fragmentation Required if the size of the multicast data packet is 633.\" too large (see "Encapsulating data packets in the Register Tunnel" 634.\" in Section 4.4.1 in the PIM-SM spec 635.\" draft-ietf-pim-sm-v2-new-05.{txt,ps}). 636.\" For IPv4 we may be able to get away without it, but for IPv6 we need 637.\" that. 638.\" 639.\" 4. Mechanism for "multicast bandwidth monitoring and upcalls". 640.\" 641.Pp 642Typically, a multicast routing user-level process would need to know the 643forwarding bandwidth for some data flow. 644For example, the multicast routing process may want to timeout idle MFC 645entries, or in case of PIM-SM it can initiate (S,G) shortest-path switch if 646the bandwidth rate is above a threshold for example. 647.Pp 648The original solution for measuring the bandwidth of a dataflow was 649that a user-level process would periodically 650query the kernel about the number of forwarded packets/bytes per 651(S,G), and then based on those numbers it would estimate whether a source 652has been idle, or whether the source's transmission bandwidth is above a 653threshold. 654That solution is far from being scalable, hence the need for a new 655mechanism for bandwidth monitoring. 656.Pp 657Below is a description of the bandwidth monitoring mechanism. 658.Bl -bullet 659.It 660If the bandwidth of a data flow satisfies some pre-defined filter, 661the kernel delivers an upcall on the multicast routing socket 662to the multicast routing process that has installed that filter. 663.It 664The bandwidth-upcall filters are installed per (S,G). 665There can be 666more than one filter per (S,G). 667.It 668Instead of supporting all possible comparison operations 669(i.e., < <= == != > >= ), there is support only for the 670<= and >= operations, 671because this makes the kernel-level implementation simpler, 672and because practically we need only those two. 673Further, the missing operations can be simulated by secondary 674user-level filtering of those <= and >= filters. 675For example, to simulate !=, then we need to install filter 676.Dq bw <= 0xffffffff , 677and after an 678upcall is received, we need to check whether 679.Dq measured_bw != expected_bw . 680.It 681The bandwidth-upcall mechanism is enabled by 682setsockopt(MRT_API_CONFIG) for the MRT_MFC_BW_UPCALL flag. 683.It 684The bandwidth-upcall filters are added/deleted by the new 685setsockopt(MRT_ADD_BW_UPCALL) and setsockopt(MRT_DEL_BW_UPCALL) 686respectively (with the appropriate 687.Dq struct bw_upcall 688argument of course). 689.El 690.Pp 691From application point of view, a developer needs to know about 692the following: 693.Bd -literal 694/* 695 * Structure for installing or delivering an upcall if the 696 * measured bandwidth is above or below a threshold. 697 * 698 * User programs (e.g. daemons) may have a need to know when the 699 * bandwidth used by some data flow is above or below some threshold. 700 * This interface allows the userland to specify the threshold (in 701 * bytes and/or packets) and the measurement interval. Flows are 702 * all packet with the same source and destination IP address. 703 * At the moment the code is only used for multicast destinations 704 * but there is nothing that prevents its use for unicast. 705 * 706 * The measurement interval cannot be shorter than some Tmin (currently, 3s). 707 * The threshold is set in packets and/or bytes per_interval. 708 * 709 * Measurement works as follows: 710 * 711 * For >= measurements: 712 * The first packet marks the start of a measurement interval. 713 * During an interval we count packets and bytes, and when we 714 * pass the threshold we deliver an upcall and we are done. 715 * The first packet after the end of the interval resets the 716 * count and restarts the measurement. 717 * 718 * For <= measurement: 719 * We start a timer to fire at the end of the interval, and 720 * then for each incoming packet we count packets and bytes. 721 * When the timer fires, we compare the value with the threshold, 722 * schedule an upcall if we are below, and restart the measurement 723 * (reschedule timer and zero counters). 724 */ 725 726struct bw_data { 727 struct timeval b_time; 728 uint64_t b_packets; 729 uint64_t b_bytes; 730}; 731 732struct bw_upcall { 733 struct in_addr bu_src; /* source address */ 734 struct in_addr bu_dst; /* destination address */ 735 uint32_t bu_flags; /* misc flags (see below) */ 736#define BW_UPCALL_UNIT_PACKETS (1 << 0) /* threshold (in packets) */ 737#define BW_UPCALL_UNIT_BYTES (1 << 1) /* threshold (in bytes) */ 738#define BW_UPCALL_GEQ (1 << 2) /* upcall if bw >= threshold */ 739#define BW_UPCALL_LEQ (1 << 3) /* upcall if bw <= threshold */ 740#define BW_UPCALL_DELETE_ALL (1 << 4) /* delete all upcalls for s,d*/ 741 struct bw_data bu_threshold; /* the bw threshold */ 742 struct bw_data bu_measured; /* the measured bw */ 743}; 744 745/* max. number of upcalls to deliver together */ 746#define BW_UPCALLS_MAX 128 747/* min. threshold time interval for bandwidth measurement */ 748#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC 3 749#define BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC 0 750.Ed 751.Pp 752The 753.Dq bw_upcall 754structure is used as an argument to 755setsockopt(MRT_ADD_BW_UPCALL) and setsockopt(MRT_DEL_BW_UPCALL). 756Each setsockopt(MRT_ADD_BW_UPCALL) installs a filter in the kernel 757for the source and destination address in the 758.Dq bw_upcall 759argument, 760and that filter will trigger an upcall according to the following 761pseudo-algorithm: 762.Bd -literal 763 if (bw_upcall_oper IS ">=") { 764 if (((bw_upcall_unit & PACKETS == PACKETS) && 765 (measured_packets >= threshold_packets)) || 766 ((bw_upcall_unit & BYTES == BYTES) && 767 (measured_bytes >= threshold_bytes))) 768 SEND_UPCALL("measured bandwidth is >= threshold"); 769 } 770 if (bw_upcall_oper IS "<=" && measured_interval >= threshold_interval) { 771 if (((bw_upcall_unit & PACKETS == PACKETS) && 772 (measured_packets <= threshold_packets)) || 773 ((bw_upcall_unit & BYTES == BYTES) && 774 (measured_bytes <= threshold_bytes))) 775 SEND_UPCALL("measured bandwidth is <= threshold"); 776 } 777.Ed 778.Pp 779In the same 780.Dq bw_upcall 781the unit can be specified in both BYTES and PACKETS. 782However, the GEQ and LEQ flags are mutually exclusive. 783.Pp 784Basically, an upcall is delivered if the measured bandwidth is >= or 785<= the threshold bandwidth (within the specified measurement 786interval). 787For practical reasons, the smallest value for the measurement 788interval is 3 seconds. 789If smaller values are allowed, then the bandwidth 790estimation may be less accurate, or the potentially very high frequency 791of the generated upcalls may introduce too much overhead. 792For the >= operation, the answer may be known before the end of 793.Dq threshold_interval , 794therefore the upcall may be delivered earlier. 795For the <= operation however, we must wait 796until the threshold interval has expired to know the answer. 797.Pp 798Example of usage: 799.Bd -literal 800struct bw_upcall bw_upcall; 801/* Assign all bw_upcall fields as appropriate */ 802memset(&bw_upcall, 0, sizeof(bw_upcall)); 803memcpy(&bw_upcall.bu_src, &source, sizeof(bw_upcall.bu_src)); 804memcpy(&bw_upcall.bu_dst, &group, sizeof(bw_upcall.bu_dst)); 805bw_upcall.bu_threshold.b_data = threshold_interval; 806bw_upcall.bu_threshold.b_packets = threshold_packets; 807bw_upcall.bu_threshold.b_bytes = threshold_bytes; 808if (is_threshold_in_packets) 809 bw_upcall.bu_flags |= BW_UPCALL_UNIT_PACKETS; 810if (is_threshold_in_bytes) 811 bw_upcall.bu_flags |= BW_UPCALL_UNIT_BYTES; 812do { 813 if (is_geq_upcall) { 814 bw_upcall.bu_flags |= BW_UPCALL_GEQ; 815 break; 816 } 817 if (is_leq_upcall) { 818 bw_upcall.bu_flags |= BW_UPCALL_LEQ; 819 break; 820 } 821 return (ERROR); 822} while (0); 823setsockopt(mrouter_s4, IPPROTO_IP, MRT_ADD_BW_UPCALL, 824 (void *)&bw_upcall, sizeof(bw_upcall)); 825.Ed 826.Pp 827To delete a single filter, then use MRT_DEL_BW_UPCALL, 828and the fields of bw_upcall must be set 829exactly same as when MRT_ADD_BW_UPCALL was called. 830.Pp 831To delete all bandwidth filters for a given (S,G), then 832only the 833.Dq bu_src 834and 835.Dq bu_dst 836fields in 837.Dq struct bw_upcall 838need to be set, and then just set only the 839.Dq BW_UPCALL_DELETE_ALL 840flag inside field 841.Dq bw_upcall.bu_flags . 842.Pp 843The bandwidth upcalls are received by aggregating them in the new upcall 844message: 845.Bd -literal 846#define IGMPMSG_BW_UPCALL 4 /* BW monitoring upcall */ 847.Ed 848.Pp 849This message is an array of 850.Dq struct bw_upcall 851elements (up to BW_UPCALLS_MAX = 128). 852The upcalls are 853delivered when there are 128 pending upcalls, or when 1 second has 854expired since the previous upcall (whichever comes first). 855In an 856.Dq struct upcall 857element, the 858.Dq bu_measured 859field is filled-in to 860indicate the particular measured values. 861However, because of the way 862the particular intervals are measured, the user should be careful how 863bu_measured.b_time is used. 864For example, if the 865filter is installed to trigger an upcall if the number of packets 866is >= 1, then 867.Dq bu_measured 868may have a value of zero in the upcalls after the 869first one, because the measured interval for >= filters is 870.Dq clocked 871by the forwarded packets. 872Hence, this upcall mechanism should not be used for measuring 873the exact value of the bandwidth of the forwarded data. 874To measure the exact bandwidth, the user would need to 875get the forwarded packets statistics with the ioctl(SIOCGETSGCNT) 876mechanism 877(see the 878.Sx Programming Guide 879section) . 880.Pp 881Note that the upcalls for a filter are delivered until the specific 882filter is deleted, but no more frequently than once per 883.Dq bu_threshold.b_time . 884For example, if the filter is specified to 885deliver a signal if bw >= 1 packet, the first packet will trigger a 886signal, but the next upcall will be triggered no earlier than 887.Dq bu_threshold.b_time 888after the previous upcall. 889.Pp 890.\" 891.Sh SEE ALSO 892.Xr getsockopt 2 , 893.Xr recvfrom 2 , 894.Xr recvmsg 2 , 895.Xr setsockopt 2 , 896.Xr socket 2 , 897.Xr icmp6 4 , 898.Xr inet 4 , 899.Xr inet6 4 , 900.Xr intro 4 , 901.Xr ip 4 , 902.Xr ip6 4 , 903.Xr pim 4 904.\" 905.Pp 906.Sh AUTHORS 907.An -nosplit 908The original multicast code was written by 909.An David Waitzman 910(BBN Labs), 911and later modified by the following individuals: 912.An Steve Deering 913(Stanford), 914.An Mark J. Steiglitz 915(Stanford), 916.An Van Jacobson 917(LBL), 918.An Ajit Thyagarajan 919(PARC), 920.An Bill Fenner 921(PARC). 922The IPv6 multicast support was implemented by the KAME project 923(http://www.kame.net), and was based on the IPv4 multicast code. 924The advanced multicast API and the multicast bandwidth 925monitoring were implemented by 926.An Pavlin Radoslavov 927(ICSI) 928in collaboration with 929.An Chris Brown 930(NextHop). 931.Pp 932This manual page was written by 933.An Pavlin Radoslavov 934(ICSI). 935