drouting_admin.xml 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Drouting Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <section>
  14. <title>Introduction</title>
  15. <para>Dynamic Routing is a module for selecting (based on multiple
  16. criteria) the the best gateway/destination to be used for delivering a
  17. certain call. Least Cost Routing (LCR) is a special case of dynamic
  18. routing - when the rules are ordered based on costs. Dynamic Routing
  19. comes with many features regarding routing rule selection:
  20. </para>
  21. <itemizedlist>
  22. <listitem><para>prefix based</para></listitem>
  23. <listitem><para>caller/group based</para></listitem>
  24. <listitem><para>time based</para></listitem>
  25. <listitem><para>priority based</para></listitem>
  26. </itemizedlist>
  27. <para>
  28. , processing :
  29. </para>
  30. <itemizedlist>
  31. <listitem><para>stripping and prefixing</para></listitem>
  32. <listitem><para>default rules</para></listitem>
  33. <listitem><para>inbound and outbound processing</para></listitem>
  34. <listitem><para>script route triggering</para></listitem>
  35. </itemizedlist>
  36. <para>
  37. and failure handling:
  38. <itemizedlist>
  39. <listitem><para>serial forking</para></listitem>
  40. <listitem><para>weight based GW selection</para></listitem>
  41. <listitem><para>random GW selection</para></listitem>
  42. </itemizedlist>
  43. </para>
  44. </section>
  45. <section>
  46. <title>Features</title>
  47. <para>
  48. The dynamic routing implementation for &kamailio; is designed with the
  49. following properties:
  50. </para>
  51. <itemizedlist>
  52. <listitem>
  53. <para>
  54. routing info (destinations, rules, groups) are stored in a database and
  55. loaded into memory at start up time; reload at runtime via RPC command
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. load balancing or random selection of the destinations (from a given set)
  61. </para>
  62. </listitem>
  63. <listitem>
  64. <para>
  65. able to handle large volume of routing info (300K of rules) with minimal
  66. speed/time and memory consumption penalties
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. script integration - Pseudo variables support in functions; scripting
  72. route triggering when rules are matched
  73. </para>
  74. </listitem>
  75. <listitem>
  76. <para>
  77. bidirectional behavior - inbound and outbound processing (strip and
  78. prefixing when sending and receiving from a destination/GW)
  79. </para>
  80. </listitem>
  81. </itemizedlist>
  82. </section>
  83. <section>
  84. <title>Performance</title>
  85. <para>
  86. There were several tests performed regarding the performance of the module
  87. when dealing with a large number of routing rules.
  88. </para>
  89. <para>
  90. The tests were performed with a set of 383000 rules and to values were
  91. measured:
  92. </para>
  93. <itemizedlist>
  94. <listitem><para>time to load from DB</para></listitem>
  95. <listitem><para>used shared memory</para></listitem>
  96. </itemizedlist>
  97. <para>
  98. The time to load was varying between 4 seconds and 8 seconds, depending of
  99. the caching of the DB client - the first load was the slowest (as the DB
  100. query hits the disk drive); the following are faster as data is already
  101. cached in the DB client. So technically speaking, the time to load (without
  102. the time to query which is DB type dependent) is ~4 seconds
  103. </para>
  104. <para>
  105. After loading the data into shared memory ~ 96M of memory were used
  106. exclusively for the DR data.
  107. </para>
  108. </section>
  109. <section>
  110. <title>Routing Rule Definition</title>
  111. <para>
  112. Dynamic routing rules are stored in a database, in four tables:
  113. </para>
  114. <itemizedlist>
  115. <listitem>
  116. <para>one for storing the gateway definitions
  117. </para>
  118. </listitem>
  119. <listitem>
  120. <para>one for storing the routing rule definitions
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>one for storing the users mappings over groups
  125. </para>
  126. </listitem>
  127. <listitem>
  128. <para>one for storing a list of gateways, so you don't have to enter all
  129. the elements every time you need it
  130. </para>
  131. </listitem>
  132. </itemizedlist>
  133. <section>
  134. <title>Gateway Addresses</title>
  135. <para>
  136. Default name for the table storing gateway addresses is
  137. <quote>dr_gateways</quote>.
  138. Gateway addresses are stored in a separate table because of need to
  139. access them independent of Dynamic Routing processing (e.g., adding/
  140. removing gateway PRI prefix before/after performing other operation
  141. -- receiving/relaying to gateway).
  142. </para>
  143. <para>
  144. <table>
  145. <title>Definition of table dr_gateways</title>
  146. <tgroup cols="4">
  147. <thead>
  148. <row>
  149. <entry>Column name</entry>
  150. <entry>Type</entry>
  151. <entry>Default value</entry>
  152. <entry>Description</entry>
  153. </row>
  154. </thead>
  155. <tbody>
  156. <row>
  157. <entry>gwid</entry>
  158. <entry>integer</entry>
  159. <entry>auto increment</entry>
  160. <entry>unique identifier for GW</entry>
  161. </row>
  162. <row>
  163. <entry>type</entry>
  164. <entry>unsigned int</entry>
  165. <entry>0</entry>
  166. <entry>type/class of GW</entry>
  167. </row>
  168. <row>
  169. <entry>address</entry>
  170. <entry>varchar(128)</entry>
  171. <entry></entry>
  172. <entry>address of the gateway</entry>
  173. </row>
  174. <row>
  175. <entry>strip</entry>
  176. <entry>unsigned int</entry>
  177. <entry>0</entry>
  178. <entry>no of digits to strip</entry>
  179. </row>
  180. <row>
  181. <entry>pri_prefix</entry>
  182. <entry>varchar(255)</entry>
  183. <entry></entry>
  184. <entry>PRI prefix of the gateway</entry>
  185. </row>
  186. <row>
  187. <entry>description</entry>
  188. <entry>varchar(128)</entry>
  189. <entry></entry>
  190. <entry>description of the gateway</entry>
  191. </row>
  192. </tbody>
  193. </tgroup>
  194. </table>
  195. </para>
  196. <para>
  197. Once a rule is matched, the STRIP number of digits are removed from the
  198. username part of the RURI and then the PRI prefix has to be added to the
  199. request URI before forwarding the call to the gateway.
  200. </para>
  201. <para>
  202. <table>
  203. <title>Sample dr_gateways records</title>
  204. <tgroup cols="6">
  205. <thead>
  206. <row>
  207. <entry>gwid</entry>
  208. <entry>type</entry>
  209. <entry>address</entry>
  210. <entry>strip</entry>
  211. <entry>pri_prefix</entry>
  212. <entry>description</entry>
  213. </row>
  214. </thead>
  215. <tbody>
  216. <row>
  217. <entry>1</entry>
  218. <entry>10</entry>
  219. <entry>10.10.10.10:5080</entry>
  220. <entry>0</entry>
  221. <entry>2222</entry>
  222. <entry>Gateway 1</entry>
  223. </row>
  224. <row>
  225. <entry>2</entry>
  226. <entry>10</entry>
  227. <entry>10.10.10.10</entry>
  228. <entry>2</entry>
  229. <entry>3333</entry>
  230. <entry>Gateway 2</entry>
  231. </row>
  232. <row>
  233. <entry>3</entry>
  234. <entry>20</entry>
  235. <entry>10.10.10.11</entry>
  236. <entry>0</entry>
  237. <entry></entry>
  238. <entry>Gateway 3</entry>
  239. </row>
  240. </tbody>
  241. </tgroup>
  242. </table>
  243. </para>
  244. </section>
  245. <section>
  246. <title>Destination/GW lists</title>
  247. <para>
  248. For each rule, you can set a list of destinations to be used. The list is
  249. comma or pipe separated enumeration of the destinations. The module
  250. will use (one by one) each destination from the list (in the given order).
  251. </para>
  252. <para>
  253. Also the module allows the usage of groups in the destination lists. A
  254. group of destinations is delimited by semi-colon char. inside the whole
  255. destination list ( like: 2,4;5,78,23;4;7;2 ). The destinations from
  256. within a group may be act differently (like load-balancing, random
  257. selection, etc), depending of the <quote>sort_order</quote> module
  258. parameter - more about this is available under the module paramters
  259. section.
  260. </para>
  261. </section>
  262. <section>
  263. <title>Routing Rules</title>
  264. <para>
  265. Default name for the table storing rule definitions is
  266. <quote>dr_rules</quote>.
  267. </para>
  268. <para>
  269. <table>
  270. <title>Definition of dr_rules table</title>
  271. <tgroup cols="4">
  272. <thead>
  273. <row>
  274. <entry>Column name</entry>
  275. <entry>Type</entry>
  276. <entry>Default</entry>
  277. <entry>Description</entry>
  278. </row>
  279. </thead>
  280. <tbody>
  281. <row>
  282. <entry>ruleid</entry>
  283. <entry>integer</entry>
  284. <entry>auto</entry>
  285. <entry>UID of the rule</entry>
  286. </row>
  287. <row>
  288. <entry>groupid</entry>
  289. <entry>varchar(255)</entry>
  290. <entry></entry>
  291. <entry>list of routing group IDs</entry>
  292. </row>
  293. <row>
  294. <entry>prefix</entry>
  295. <entry>varchar(64)</entry>
  296. <entry></entry>
  297. <entry>destination prefix for this route</entry>
  298. </row>
  299. <row>
  300. <entry>timerec</entry>
  301. <entry>varchar(255)</entry>
  302. <entry></entry>
  303. <entry>time recurrence for this rule</entry>
  304. </row>
  305. <row>
  306. <entry>priority</entry>
  307. <entry>integer</entry>
  308. <entry>0</entry>
  309. <entry>priority of the rule</entry>
  310. </row>
  311. <row>
  312. <entry>routeid</entry>
  313. <entry>integer</entry>
  314. <entry>0</entry>
  315. <entry>route block to be execute</entry>
  316. </row>
  317. <row>
  318. <entry>gwlist</entry>
  319. <entry>varchar(255)</entry>
  320. <entry></entry>
  321. <entry>the list with GWs to be used</entry>
  322. </row>
  323. <row>
  324. <entry>description</entry>
  325. <entry>varchar(128)</entry>
  326. <entry></entry>
  327. <entry>description of this rule</entry>
  328. </row>
  329. </tbody>
  330. </tgroup>
  331. </table>
  332. </para>
  333. <para>
  334. <orderedlist numeration="loweralpha">
  335. <listitem>
  336. <para>
  337. <emphasis>groupid column</emphasis>
  338. </para>
  339. <para>
  340. Each user must be member of only one routing group. It must be
  341. specified in user's profile.
  342. </para>
  343. </listitem>
  344. <listitem>
  345. <para>
  346. <emphasis>prefix column</emphasis>
  347. </para>
  348. <para>
  349. Destination URI must start with prefix value to match the rule.
  350. </para>
  351. </listitem>
  352. <listitem>
  353. <para>
  354. <emphasis>time rec column</emphasis>
  355. </para>
  356. <para>
  357. A date-time expression that defines the time recurrence to match for
  358. current rule. Time recurrences are based closely on the specification
  359. of recurring intervals of time in the Internet Calendaring and Scheduling
  360. Core Object Specification (calendar COS), RFC 2445. The set of attributes
  361. used in routing rule specification is subset of time recurrence attributes
  362. used by Call Processing Language (CPL). These attributes are (extracted
  363. from CPL draft 09):
  364. </para>
  365. <para>
  366. <table>
  367. <title>Time recurrence attributes</title>
  368. <tgroup cols="2">
  369. <thead>
  370. <row>
  371. <entry>Attribute</entry>
  372. <entry>Description</entry>
  373. </row>
  374. </thead>
  375. <tbody>
  376. <row>
  377. <entry>dastard</entry>
  378. <entry>Start of interval (RFC 2445 DATE-TIME)</entry>
  379. </row>
  380. <row>
  381. <entry>duration</entry>
  382. <entry>Length of interval (RFC 2445 DURATION)</entry>
  383. </row>
  384. <row>
  385. <entry>freq</entry>
  386. <entry>Frequency of recurrence (secondly,minutely,hourly, daily,weekly,
  387. monthly, or yearly).</entry>
  388. </row>
  389. <row>
  390. <entry>until</entry>
  391. <entry>bound of recurrence (RFC 2445 DATE-TIME)</entry>
  392. </row>
  393. <row>
  394. <entry>interval</entry>
  395. <entry>How often the recurrence repeats</entry>
  396. </row>
  397. <row>
  398. <entry>byday</entry>
  399. <entry>List of days of the week</entry>
  400. </row>
  401. <row>
  402. <entry>bymonthday</entry>
  403. <entry>List of days of the month</entry>
  404. </row>
  405. <row>
  406. <entry>byyearday</entry>
  407. <entry>List of days of the year</entry>
  408. </row>
  409. <row>
  410. <entry>byweekno</entry>
  411. <entry>List of weeks of the year</entry>
  412. </row>
  413. <row>
  414. <entry>bymonth</entry>
  415. <entry>List of months of the year</entry>
  416. </row>
  417. </tbody>
  418. </tgroup>
  419. </table>
  420. </para>
  421. <para>
  422. The value stored in database has the format of:
  423. <![CDATA[
  424. <dtstart>|<duration>|<freq>|<until>|<interval>|<byday>|<bymonthday>|<byyearday>|<byweekno>|<bymonth>
  425. ]]>
  426. </para>
  427. <para>
  428. When an attribute is not specified, the corresponding place must be left
  429. empty, whenever another attribute that follows in the list has to be
  430. specified.
  431. </para>
  432. <para>
  433. Detailed description of time recurrence attributes:
  434. <itemizedlist>
  435. <listitem>
  436. <para>
  437. <emphasis>dtstart</emphasis> - specifies the beginning of the first
  438. period.
  439. </para>
  440. </listitem>
  441. <listitem>
  442. <para>
  443. <emphasis>duration</emphasis> - specifies the duration of the period.
  444. For a recurring interval, the <quote>duration</quote> parameter MUST
  445. be small enough such that subsequent intervals do not overlap.
  446. For non-recurring intervals, durations of any positive length are
  447. permitted, zero-length duration means <quote>forever</quote>.
  448. Negative-length durations are not allowed.
  449. </para>
  450. </listitem>
  451. <listitem>
  452. <para>
  453. <emphasis>freq</emphasis> - takes one of the following values:
  454. <quote>daily</quote>,
  455. to specify repeating periods based on an interval of a day or more;
  456. <quote>weekly</quote>, to specify repeating periods based on an
  457. interval of a week or more; <quote>monthly</quote>, to specify
  458. repeating periods based on an interval of a month or more; and
  459. <quote>yearly</quote>, to specify repeating periods based
  460. on an interval of a year or more. These values are not case-sensitive.
  461. </para>
  462. </listitem>
  463. <listitem>
  464. <para>
  465. <emphasis>until</emphasis> - defines an iCalendar COS DATE or DATE-TIME
  466. value which bounds the recurrence rule in an inclusive manner. If the
  467. value specified by <quote>until</quote> is synchronized with the
  468. specified
  469. recurrence, this date or date-time becomes the last instance of the
  470. recurrence. If not present, the recurrence is considered to repeat
  471. forever.
  472. </para>
  473. </listitem>
  474. <listitem>
  475. <para>
  476. <emphasis>interval</emphasis> - contains a positive integer
  477. representing how often the recurrence rule repeats. The default value
  478. is <quote>1</quote>, meaning every day for a <quote>daily</quote> rule,
  479. every week for a <quote>weekly</quote>
  480. rule, every month for a <quote>monthly</quote> rule and every year for
  481. a <quote>yearly</quote> rule.
  482. </para>
  483. </listitem>
  484. <listitem>
  485. <para>
  486. <emphasis>interval</emphasis> - contains a positive integer
  487. representing how often the recurrence rule repeats. The default value
  488. is <quote>1</quote>, meaning every day for a <quote>daily</quote> rule,
  489. every week for a <quote>weekly</quote> rule, every
  490. month for a <quote>monthly</quote> rule and every year for a
  491. <quote>yearly</quote> rule.
  492. </para>
  493. </listitem>
  494. <listitem>
  495. <para>
  496. <emphasis>byday</emphasis> - specifies a comma-separated list of days
  497. of the week. <quote>MO</quote> indicates Monday; <quote>TU</quote>
  498. indicates Tuesday; <quote>WE</quote> indicates Wednesday;
  499. <quote>TH</quote> indicates Thursday; <quote>FR</quote> indicates
  500. Friday; <quote>SA</quote> indicates Saturday; <quote>SU</quote>
  501. indicates Sunday. These values are not case-sensitive.
  502. </para>
  503. <para>
  504. Each <quote>byday</quote> value can also be preceded by a positive
  505. (+n) or negative (-n) integer. If present, this indicates the nth
  506. occurrence of the specific day within the <quote>monthly</quote> or
  507. <quote>yearly</quote> recurrence. For example, within a
  508. <quote>monthly</quote> rule, +1MO (or simply 1MO) represents the first
  509. Monday within the month, whereas -1MO represents the last Monday of
  510. the month. If an integer modifier is not present, it means all days
  511. of this type within the specified frequency. For example, within a
  512. <quote>monthly</quote> rule, MO represents all Mondays within the month.
  513. </para>
  514. </listitem>
  515. <listitem>
  516. <para>
  517. <emphasis>bymonthday</emphasis> - parameter specifies a comma-separated
  518. list of days of the month. Valid values are 1 to 31 or -31 to -1. For
  519. example, -10 represents the tenth to the last day of the month.
  520. </para>
  521. </listitem>
  522. <listitem>
  523. <para>
  524. <emphasis>byyearday</emphasis> - specifies a comma-separated list of
  525. days of the year. Valid values are 1 to 366 or -366 to -1. For example,
  526. -1 represents the last day of the year (December 31st) and -306
  527. represents the 306th to the last day of the year (March 1st).
  528. </para>
  529. </listitem>
  530. <listitem>
  531. <para>
  532. <emphasis>byweekno</emphasis> - specifies a comma-separated list of
  533. ordinals specifying weeks of the year. Valid values are 1 to 53 or
  534. -53 to -1.
  535. </para>
  536. </listitem>
  537. <listitem>
  538. <para>
  539. <emphasis>bymonth</emphasis> - parameter specifies a comma-separated
  540. list of months of the year. Valid values are 1 to 12.
  541. </para>
  542. </listitem>
  543. </itemizedlist>
  544. </para>
  545. <para>
  546. </para>
  547. <para></para>
  548. <para>
  549. A recurrence is specified by including the <quote>freq</quote>
  550. parameter, which indicates the type of recurrence rule. Parameters
  551. other than <quote>dtstart</quote>
  552. and <quote>duration</quote> SHOULD NOT be specified unless
  553. <quote>freq</quote> is present.
  554. </para>
  555. <para></para>
  556. <para>
  557. If byxxx parameter values are found which are beyond the available
  558. scope (ie, bymonthday=<quote>30</quote> in February), they are simply
  559. ignored.
  560. </para>
  561. <sbr></sbr>
  562. <para></para>
  563. <para>
  564. Byxxx parameters modify the recurrence in some manner. Byxxx rule
  565. parts for a period of time which is the same or greater than the
  566. frequency generally reduce or limit the number of occurrences of the
  567. recurrence generated. For example, freq=<quote>daily</quote>
  568. bymonth=<quote>1</quote> reduces the number of
  569. recurrence instances from all days (if the <quote>bymonth</quote>
  570. parameter is not present) to all days in January. Byxxx parameters for
  571. a period of time less than the frequency generally increase or expand
  572. the number of occurrences of the recurrence. For example,
  573. freq=<quote>yearly</quote> bymonth=<quote>1,2</quote>
  574. increases the number of days within the yearly recurrence set from 1
  575. (if <quote>bymonth</quote> parameter is not present) to 2.
  576. </para>
  577. <para>
  578. If multiple Byxxx parameters are specified, then after evaluating the
  579. specified <quote>freq</quote> and <quote>interval</quote> parameters,
  580. the Byxxx parameters are
  581. applied to the current set of evaluated occurrences in the following
  582. order: <quote>bymonth</quote>, <quote>byweekno</quote>,
  583. <quote>byyearday</quote>, <quote>bymonthday</quote>,
  584. <quote>byday</quote>; then <quote>until</quote> is evaluated.
  585. </para>
  586. <para>
  587. Here is an example of evaluating multiple Byxxx parameters.
  588. </para>
  589. <para>
  590. dtstart=<quote>19970105T083000</quote> duration=<quote>10M</quote>
  591. freq=<quote>yearly</quote> interval=<quote>2</quote>
  592. bymonth=<quote>1</quote> byday=<quote>SU</quote>
  593. </para>
  594. <para>
  595. First, the interval=<quote>2</quote> would be applied to
  596. freq=<quote>yearly</quote> to arrive at <quote>every other year</quote>
  597. . Then, bymonth=<quote>1</quote> would be applied to arrive at
  598. <quote>every January, every other year</quote>. Then,
  599. byday=<quote>SU</quote> would be applied to arrive at <quote>every
  600. Sunday in January,
  601. every other year, from 8:30 to 8:40 </quote>. The appropriate minutes
  602. and hours have been retrieved from the <quote>dtstart</quote> and
  603. <quote>duration</quote> parameters.
  604. </para>
  605. </listitem>
  606. <listitem>
  607. <para>
  608. <emphasis>priority column</emphasis>
  609. </para>
  610. <para>
  611. If many rules are eligible, choose the one with highest priority.
  612. </para>
  613. </listitem>
  614. <listitem>
  615. <para>
  616. <emphasis>routeid column</emphasis>
  617. </para>
  618. <para>
  619. If different than 0, then execute the route with the specified ID.
  620. That is, a route which can be used to perform custom
  621. operations on message. At this route, no modification is performed
  622. at signaling level.
  623. </para>
  624. </listitem>
  625. <listitem>
  626. <para>
  627. <emphasis>gwlist column</emphasis>
  628. </para>
  629. <para>
  630. A comma separated list of gateway identifiers corresponding to a row in
  631. table <quote>dr_gateways</quote>. You can use a predefined list from the
  632. table <quote>dr_gw_lists</quote> preceded by the character
  633. <quote>#</quote>. The first gateway is tried first and if routing to it
  634. fails, then the second one, and so one. If no gateway is left a negative
  635. response is sent back to caller.
  636. </para>
  637. </listitem>
  638. <listitem>
  639. <para>
  640. <emphasis>Routing Rules Examples</emphasis>
  641. </para>
  642. <para>
  643. <table align="center" frame= "all" pgwide="1">
  644. <title>Sample dr_rules records</title>
  645. <tgroup cols="8" align='left' colsep='1' rowsep='1'>
  646. <thead>
  647. <row>
  648. <entry>ruleid</entry>
  649. <entry>group</entry>
  650. <entry>prefix</entry>
  651. <entry>timerec</entry>
  652. <entry>priority</entry>
  653. <entry>routeid</entry>
  654. <entry>gwlist</entry>
  655. <entry>description</entry>
  656. </row>
  657. </thead>
  658. <tbody>
  659. <row>
  660. <entry>1</entry>
  661. <entry>6</entry>
  662. <entry>0049</entry>
  663. <entry>20040101T083000|10H|weekly|||MO,TU,WE,TH,FR</entry>
  664. <entry>5</entry>
  665. <entry>23</entry>
  666. <entry>1,2</entry>
  667. <entry>Rule 1</entry>
  668. </row>
  669. <row>
  670. <entry>2</entry>
  671. <entry>8</entry>
  672. <entry>0049</entry>
  673. <entry>20040101T083000</entry>
  674. <entry>0</entry>
  675. <entry>0</entry>
  676. <entry>1,2</entry>
  677. <entry>Rule 2</entry>
  678. </row>
  679. <row>
  680. <entry>3</entry>
  681. <entry>7,8,9</entry>
  682. <entry>0049</entry>
  683. <entry>20040101T083000</entry>
  684. <entry>0</entry>
  685. <entry>0</entry>
  686. <entry>3</entry>
  687. <entry>Rule 3</entry>
  688. </row>
  689. </tbody>
  690. </tgroup>
  691. </table>
  692. </para>
  693. <para>
  694. (The time recurrence for first rule is:
  695. <quote>20040101T083000|10H|weekly|||MO,TU,WE,TH,FR</quote>)
  696. </para>
  697. </listitem>
  698. </orderedlist>
  699. </para>
  700. </section>
  701. </section>
  702. <section>
  703. <title>Routing Rule Processing</title>
  704. <para>
  705. The module can be used to find out which is the best gateway to use for new
  706. calls terminated to PSTN. The algorithm to select the rule is as follows:
  707. <itemizedlist mark='bullet'>
  708. <listitem>
  709. <para>
  710. the module discovers the routing group of the originating user. This
  711. step is skipped if a routing group is passed from the script as parameter.
  712. </para>
  713. </listitem>
  714. <listitem>
  715. <para>
  716. once the group is known, in the subset of the rules for this group the
  717. module looks for the one that matches the destination based on "prefix"
  718. column. The set of rules with the longest prefix is chosen. If no digit
  719. from the prefix matches, the default rules are used (rules with no prefix)
  720. </para>
  721. </listitem>
  722. <listitem>
  723. <para>
  724. within the set of rules is applied the time criteria, and the rule which
  725. has the highest priority and matches the time criteria is selected to drive
  726. the routing.
  727. </para>
  728. </listitem>
  729. <listitem>
  730. <para>
  731. Once found the rule, it may contain a route ID to execute. If a certain
  732. flag is set, then the processing is stopped after executing the route
  733. block.
  734. </para>
  735. </listitem>
  736. <listitem>
  737. <para>
  738. The rule must contain a gateway chain. The module will execute serial
  739. forking for each address in chain. The next address in chain is used only
  740. if the previously has failed.
  741. </para>
  742. </listitem>
  743. <listitem>
  744. <para>
  745. With the right gateway address found, the prefix (PRI) of the gateway is
  746. added to the request URI and then the request is forwarded.
  747. </para>
  748. </listitem>
  749. </itemizedlist>
  750. </para>
  751. <para>
  752. If no rule is found to match the selection criteria an default action must
  753. be taken (e.g., error response sent back). If the gateway in the chain has
  754. no prefix the request is forwarded without adding any prefix to the request
  755. URI.
  756. </para>
  757. </section>
  758. </section>
  759. <section>
  760. <title>Dependencies</title>
  761. <section>
  762. <title>&kamailio; Modules</title>
  763. <para>
  764. The following modules must be loaded before this module:
  765. <itemizedlist>
  766. <listitem>
  767. <para>
  768. <emphasis>a database module</emphasis>.
  769. </para>
  770. </listitem>
  771. </itemizedlist>
  772. </para>
  773. </section>
  774. <section>
  775. <title>External Libraries or Applications</title>
  776. <itemizedlist>
  777. <listitem>
  778. <para>
  779. <emphasis>none</emphasis>.
  780. </para>
  781. </listitem>
  782. </itemizedlist>
  783. </section>
  784. </section>
  785. <section>
  786. <title>Parameters</title>
  787. <section>
  788. <title><varname>db_url</varname>(str)</title>
  789. <para>
  790. The database url.
  791. </para>
  792. <para>
  793. <emphasis> Default value is <quote>NULL</quote>.
  794. </emphasis>
  795. </para>
  796. <example>
  797. <title>Set <varname>db_url</varname> parameter</title>
  798. <programlisting format="linespecific">
  799. ...
  800. modparam("drouting", "db_url",
  801. "&defaultdb;")
  802. ...
  803. </programlisting>
  804. </example>
  805. </section>
  806. <section>
  807. <title><varname>drd_table</varname>(str)</title>
  808. <para>
  809. The name of the db table storing gateway addresses.
  810. </para>
  811. <para>
  812. <emphasis> Default value is <quote>dr_gateways</quote>.
  813. </emphasis>
  814. </para>
  815. <example>
  816. <title>Set <varname>drd_table</varname> parameter</title>
  817. <programlisting format="linespecific">
  818. ...
  819. modparam("drouting", "drd_table", "dr_gateways")
  820. ...
  821. </programlisting>
  822. </example>
  823. </section>
  824. <section>
  825. <title><varname>drr_table</varname>(str)</title>
  826. <para>
  827. The name of the db table storing routing rules.
  828. </para>
  829. <para>
  830. <emphasis> Default value is <quote>dr_rules</quote>.
  831. </emphasis>
  832. </para>
  833. <example>
  834. <title>Set <varname>drr_table</varname> parameter</title>
  835. <programlisting format="linespecific">
  836. ...
  837. modparam("drouting", "drr_table", "rules")
  838. ...
  839. </programlisting>
  840. </example>
  841. </section>
  842. <section>
  843. <title><varname>drg_table</varname>(str)</title>
  844. <para>
  845. The name of the db table storing groups.
  846. </para>
  847. <para>
  848. <emphasis> Default value is <quote>dr_groups</quote>.
  849. </emphasis>
  850. </para>
  851. <example>
  852. <title>Set <varname>drg_table</varname> parameter</title>
  853. <programlisting format="linespecific">
  854. ...
  855. modparam("drouting", "drg_table", "groups")
  856. ...
  857. </programlisting>
  858. </example>
  859. </section>
  860. <section>
  861. <title><varname>drl_table</varname>(str)</title>
  862. <para>
  863. The name of the db table storing definitions of destination lists (to
  864. be used directly by the routing rules).
  865. You will have a identifier to a group of gateways instead of having all the
  866. members of the group as a individual elements.
  867. Very useful to reuse a list of gateways in different places.
  868. </para>
  869. <para>
  870. <emphasis> Default value is <quote>dr_gw_lists</quote>.
  871. </emphasis>
  872. </para>
  873. <example>
  874. <title>Set <varname>drl_table</varname> parameter</title>
  875. <programlisting format="linespecific">
  876. ...
  877. modparam("drouting", "drl_table", "my_gw_lists")
  878. ...
  879. </programlisting>
  880. </example>
  881. </section>
  882. <section>
  883. <title><varname>sort_order</varname> (int)</title>
  884. <para>
  885. Defines how the destination list should be processed (ordering of
  886. the elements). Possible modes are
  887. <itemizedlist>
  888. <listitem>
  889. <emphasis>0</emphasis> - destination groups are ignored and all the
  890. destinations are tried in the given order;
  891. Ex: list 1,2;3,4,5;6 will lead to usage as 1,2,3,4,5,6
  892. </listitem>
  893. <listitem>
  894. <emphasis>1</emphasis> - the destinations from each group are
  895. randomly arranged (only the two elements are randomly selected);
  896. groups do maintain their order (as given); the resulting list is
  897. used (with all the defined destinations).
  898. Ex: 1,2;3,4,5;6 -> randomizer ->
  899. (A) 2,1;4,3,5;6 -> usage 2,1,4,3,5,6
  900. (B) 1,2;3,5,4;6 -> usage 1,2,3,5,4,6
  901. </listitem>
  902. <listitem>
  903. <emphasis>2</emphasis> - from each destination group, only a
  904. single destination is randomly selected; groups do maintain their
  905. order (as given);
  906. <para>
  907. Ex: 1,2;3,4,5;6 -> randomizer ->
  908. </para>
  909. <para>
  910. (A) 2;4;6 -> usage 2,4,6
  911. </para>
  912. <para>
  913. (B) 1;5;6 -> usage 1,5,6
  914. </para>
  915. <para>
  916. It is ok to have repeating gateways in different groups. The module will
  917. take care internally in case of failure not to choose a gateway that
  918. was tried already.
  919. </para>
  920. <para>
  921. Ex: 1,2,3; 1,2,3; 1,2,3 -> no gateway will be chosen twice. So in case there
  922. are 2 failures, all the three gateways (1,2,3) will be tried in a random order.
  923. </para>
  924. </listitem>
  925. </itemizedlist>
  926. </para>
  927. <para>
  928. <emphasis>Default value is <quote>0</quote>.
  929. </emphasis>
  930. </para>
  931. <example>
  932. <title>Set <varname>sort_order</varname> parameter</title>
  933. <programlisting format="linespecific">
  934. ...
  935. modparam("drouting", "sort_order", 2)
  936. ...
  937. </programlisting>
  938. </example>
  939. </section>
  940. <section>
  941. <title><varname>ruri_avp</varname> (str)</title>
  942. <para>
  943. The name of the avp for storing Request URIs to be later used
  944. (alternative destiantions for the current one).
  945. </para>
  946. <para>
  947. <emphasis>Default value is <quote>NULL</quote>.
  948. </emphasis>
  949. </para>
  950. <example>
  951. <title>Set <varname>ruri_avp</varname> parameter</title>
  952. <programlisting format="linespecific">
  953. ...
  954. modparam("drouting", "ruri_avp", '$avp(dr_ruri)')
  955. modparam("drouting", "ruri_avp", '$avp(i:33)')
  956. ...
  957. </programlisting>
  958. </example>
  959. </section>
  960. <section>
  961. <title><varname>attrs_avp</varname> (str)</title>
  962. <para>
  963. The name of the avp for storing the attribute of the current selected
  964. destination - once a new destination is selected (via the
  965. use_next_gw() function), the AVP will be updated with the attrs of the
  966. new used destination.
  967. </para>
  968. <para>
  969. <emphasis>Default value is <quote>NULL</quote>.
  970. </emphasis>
  971. </para>
  972. <example>
  973. <title>Set <varname>attrs_avp</varname> parameter</title>
  974. <programlisting format="linespecific">
  975. ...
  976. modparam("drouting", "attrs_avp", '$avp(dr_attrs)')
  977. modparam("drouting", "atrrs_avp", '$avp(i:67)')
  978. ...
  979. </programlisting>
  980. </example>
  981. </section>
  982. <section>
  983. <title><varname>use_domain</varname> (int)</title>
  984. <para>
  985. Flag to configure whether to use domain match when querying
  986. database for user's routing group.
  987. </para>
  988. <para>
  989. <emphasis>Default value is <quote>1</quote>.
  990. </emphasis>
  991. </para>
  992. <example>
  993. <title>Set <varname>use_domain</varname> parameter</title>
  994. <programlisting format="linespecific">
  995. ...
  996. modparam("drouting", "use_domain", 0)
  997. ...
  998. </programlisting>
  999. </example>
  1000. </section>
  1001. <section>
  1002. <title><varname>drg_user_col</varname> (str)</title>
  1003. <para>
  1004. The name of the column in group db table where the username is stored.
  1005. </para>
  1006. <para>
  1007. <emphasis>Default value is <quote>username</quote>.
  1008. </emphasis>
  1009. </para>
  1010. <example>
  1011. <title>Set <varname>drg_user_col</varname> parameter</title>
  1012. <programlisting format="linespecific">
  1013. ...
  1014. modparam("drouting", "drg_user_col", "user")
  1015. ...
  1016. </programlisting>
  1017. </example>
  1018. </section>
  1019. <section>
  1020. <title><varname>drg_domain_col</varname> (str)</title>
  1021. <para>
  1022. The name of the column in group db table where the domain is stored.
  1023. </para>
  1024. <para>
  1025. <emphasis>Default value is <quote>domain</quote>.
  1026. </emphasis>
  1027. </para>
  1028. <example>
  1029. <title>Set <varname>drg_domain_col</varname> parameter</title>
  1030. <programlisting format="linespecific">
  1031. ...
  1032. modparam("drouting", "drg_domain_col", "host")
  1033. ...
  1034. </programlisting>
  1035. </example>
  1036. </section>
  1037. <section>
  1038. <title><varname>drg_grpid_col</varname> (str)</title>
  1039. <para>
  1040. The name of the column in group db table where the
  1041. group id is stored.
  1042. </para>
  1043. <para>
  1044. <emphasis>Default value is <quote>groupid</quote>.
  1045. </emphasis>
  1046. </para>
  1047. <example>
  1048. <title>Set <varname>drg_grpid_col</varname> parameter</title>
  1049. <programlisting format="linespecific">
  1050. ...
  1051. modparam("drouting", "drg_grpid_col", "grpid")
  1052. ...
  1053. </programlisting>
  1054. </example>
  1055. </section>
  1056. <section>
  1057. <title><varname>fetch_rows</varname> (int)</title>
  1058. <para>
  1059. </para>
  1060. The number of rows that should be fetched from the result of a
  1061. query in rules db table.
  1062. <para>
  1063. <emphasis>Default value is <quote>2000</quote>.
  1064. </emphasis>
  1065. </para>
  1066. <example>
  1067. <title>Set <varname>fetch_rows</varname> parameter</title>
  1068. <programlisting format="linespecific">
  1069. ...
  1070. modparam("drouting", "fetch_rows", 1500)
  1071. ...
  1072. </programlisting>
  1073. </example>
  1074. </section>
  1075. <section>
  1076. <title><varname>force_dns</varname> (int)</title>
  1077. <para>
  1078. Force DNS resolving of GW/destination names (if not IPs) during
  1079. startup. If not enabled, the GW name will be blindly used during
  1080. routing.
  1081. </para>
  1082. <para>
  1083. <emphasis>Default value is <quote>1 (enabled)</quote>.
  1084. </emphasis>
  1085. </para>
  1086. <example>
  1087. <title>Set <varname>force_dns</varname> parameter</title>
  1088. <programlisting format="linespecific">
  1089. ...
  1090. modparam("drouting", "force_dns", 0)
  1091. ...
  1092. </programlisting>
  1093. </example>
  1094. </section>
  1095. </section>
  1096. <section>
  1097. <title>Functions</title>
  1098. <section>
  1099. <title>
  1100. <function moreinfo="none">do_routing("[groupID]")</function>
  1101. </title>
  1102. <para>
  1103. Function to trigger routing of the message according to the
  1104. rules in the database table and the configured parameters.
  1105. </para>
  1106. <para>
  1107. This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
  1108. </para>
  1109. <para>
  1110. The module can take one optional parameter: the routing group the
  1111. caller belongs to - this may be a static numerical value or an AVP
  1112. specification. If none specified, the function will automatically
  1113. try to query the dr_group table to get this information.
  1114. </para>
  1115. <example>
  1116. <title><function>do_routing</function> usage</title>
  1117. <programlisting format="linespecific">
  1118. ...
  1119. do_routing();
  1120. ...
  1121. do_routing("0");
  1122. ...
  1123. do_routing("$avp(i:10)");
  1124. </programlisting>
  1125. </example>
  1126. </section>
  1127. <section>
  1128. <title>
  1129. <function moreinfo="none">use_next_gw()/next_routing()</function>
  1130. </title>
  1131. <para>
  1132. The function takes the next available destination (set by do_routing,
  1133. as alternative destinations) and push it into RURI. Note that the
  1134. function just sets the RURI (nothing more).
  1135. </para>
  1136. <para>
  1137. If a new RURI is set, the used destination is removed from the
  1138. pending set of alternative destinations.
  1139. </para>
  1140. <para>
  1141. This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
  1142. </para>
  1143. <para>
  1144. The function returns true only if a new RURI was set. False
  1145. is returned is no other alternative destinations are found or in case
  1146. of internal processing error.
  1147. </para>
  1148. <example>
  1149. <title><function>use_next_gw</function> usage</title>
  1150. <programlisting format="linespecific">
  1151. ...
  1152. if (use_next_gw()) {
  1153. t_relay();
  1154. exit;
  1155. }
  1156. ...
  1157. </programlisting>
  1158. </example>
  1159. </section>
  1160. <section>
  1161. <title>
  1162. <function moreinfo="none">goes_to_gw([type])</function>
  1163. </title>
  1164. <para>
  1165. Function returns true if the destination of the current request
  1166. (destination URI or Request URI) points (as IP) to one of the gateways.
  1167. There no DNS lookups done if the domain part of the URI is not an IP.
  1168. </para>
  1169. <para>
  1170. This function does not change anything in the message.
  1171. </para>
  1172. <para>
  1173. This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
  1174. </para>
  1175. <para>
  1176. The function can take one optional parameter:
  1177. <itemizedlist>
  1178. <listitem>
  1179. <emphasis>type</emphasis> (optional) - GW/destination
  1180. type to be checked
  1181. </listitem>
  1182. </itemizedlist>
  1183. </para>
  1184. <example>
  1185. <title><function>goes_to_gw</function> usage</title>
  1186. <programlisting format="linespecific">
  1187. ...
  1188. if (goes_to_gw("1")) {
  1189. sl_send_reply("403","Forbidden");
  1190. exit;
  1191. }
  1192. ...
  1193. </programlisting>
  1194. </example>
  1195. </section>
  1196. <section>
  1197. <title>
  1198. <function moreinfo="none">is_from_gw([type])</function>
  1199. </title>
  1200. <para>
  1201. The function checks if the sender of the message is a gateway
  1202. from a certain group.
  1203. </para>
  1204. <para>
  1205. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE and
  1206. ONREPLY_ROUTE
  1207. </para>
  1208. <para>
  1209. The function can take one optional parameter:
  1210. <itemizedlist>
  1211. <listitem>
  1212. <emphasis>type</emphasis> (optional) - GW/destination type
  1213. to be checked
  1214. </listitem>
  1215. <listitem>
  1216. <emphasis>flags</emphasis> - if message is a request and
  1217. the GW has a STRIP defined, then apply it if GW is source.
  1218. </listitem>
  1219. </itemizedlist>
  1220. </para>
  1221. <example>
  1222. <title><function>is_from_gw</function> usage</title>
  1223. <programlisting format="linespecific">
  1224. ...
  1225. if (is_from_gw("1") {
  1226. }
  1227. ...
  1228. </programlisting>
  1229. </example>
  1230. </section>
  1231. <section>
  1232. <title>
  1233. <function moreinfo="none">is_from_gw( type, [flag])</function>
  1234. </title>
  1235. <para>
  1236. The function checks if the sender of the message is a gateway
  1237. from a certain group.
  1238. </para>
  1239. <para>
  1240. This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
  1241. </para>
  1242. <para>
  1243. The function can take two parameters:
  1244. <itemizedlist>
  1245. <listitem>
  1246. <emphasis>type</emphasis> (mandatory) - GW/destination
  1247. type to be checked
  1248. </listitem>
  1249. <listitem>
  1250. <emphasis>flags</emphasis> (optional) - if message is a
  1251. request and the GW has a STRIP defined, then apply it
  1252. if GW is source.
  1253. </listitem>
  1254. </itemizedlist>
  1255. </para>
  1256. <example>
  1257. <title><function>is_from_gw</function> usage</title>
  1258. <programlisting format="linespecific">
  1259. ...
  1260. if (is_from_gw("3","1") {
  1261. }
  1262. ...
  1263. </programlisting>
  1264. </example>
  1265. </section>
  1266. </section>
  1267. <section>
  1268. <title>RPC Commands</title>
  1269. <section>
  1270. <title>
  1271. <function moreinfo="none">drouting.reload</function>
  1272. </title>
  1273. <para>Command to reload routing rules from database.</para>
  1274. <para>It takes no parameter.</para>
  1275. <para>RPC Command Format:</para>
  1276. <programlisting format="linespecific">
  1277. kamcmd drouting.reload
  1278. </programlisting>
  1279. </section>
  1280. </section>
  1281. <section>
  1282. <title>Installation</title>
  1283. <para>
  1284. The module requires 3 table in &kamailio; database: dr_groups,
  1285. dr_gateways, dr_rules. The SQL syntax to create them can be
  1286. found in drouting-create.sql script in &ctltool; db directories.
  1287. You can also find the complete
  1288. database documentation on the project webpage, &kamailiodbdocslink;.
  1289. </para>
  1290. </section>
  1291. </chapter>