pim_oil.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "log.h"
  20. #include "memory.h"
  21. #include "linklist.h"
  22. #include "pimd.h"
  23. #include "pim_oil.h"
  24. #include "pim_str.h"
  25. #include "pim_iface.h"
  26. void pim_channel_oil_free(struct channel_oil *c_oil)
  27. {
  28. XFREE(MTYPE_PIM_CHANNEL_OIL, c_oil);
  29. }
  30. static void pim_channel_oil_delete(struct channel_oil *c_oil)
  31. {
  32. /*
  33. notice that listnode_delete() can't be moved
  34. into pim_channel_oil_free() because the later is
  35. called by list_delete_all_node()
  36. */
  37. listnode_delete(qpim_channel_oil_list, c_oil);
  38. pim_channel_oil_free(c_oil);
  39. }
  40. static struct channel_oil *channel_oil_new(struct in_addr group_addr,
  41. struct in_addr source_addr,
  42. int input_vif_index)
  43. {
  44. struct channel_oil *c_oil;
  45. struct interface *ifp_in;
  46. ifp_in = pim_if_find_by_vif_index(input_vif_index);
  47. if (!ifp_in) {
  48. /* warning only */
  49. char group_str[100];
  50. char source_str[100];
  51. pim_inet4_dump("<group?>", group_addr, group_str, sizeof(group_str));
  52. pim_inet4_dump("<source?>", source_addr, source_str, sizeof(source_str));
  53. zlog_warn("%s: (S,G)=(%s,%s) could not find input interface for input_vif_index=%d",
  54. __PRETTY_FUNCTION__,
  55. source_str, group_str, input_vif_index);
  56. }
  57. c_oil = XCALLOC(MTYPE_PIM_CHANNEL_OIL, sizeof(*c_oil));
  58. if (!c_oil) {
  59. zlog_err("PIM XCALLOC(%zu) failure", sizeof(*c_oil));
  60. return 0;
  61. }
  62. c_oil->oil.mfcc_mcastgrp = group_addr;
  63. c_oil->oil.mfcc_origin = source_addr;
  64. c_oil->oil.mfcc_parent = input_vif_index;
  65. c_oil->oil_ref_count = 1;
  66. zassert(c_oil->oil_size == 0);
  67. return c_oil;
  68. }
  69. static struct channel_oil *pim_add_channel_oil(struct in_addr group_addr,
  70. struct in_addr source_addr,
  71. int input_vif_index)
  72. {
  73. struct channel_oil *c_oil;
  74. c_oil = channel_oil_new(group_addr, source_addr, input_vif_index);
  75. if (!c_oil) {
  76. zlog_warn("PIM XCALLOC(%zu) failure", sizeof(*c_oil));
  77. return 0;
  78. }
  79. listnode_add(qpim_channel_oil_list, c_oil);
  80. return c_oil;
  81. }
  82. static struct channel_oil *pim_find_channel_oil(struct in_addr group_addr,
  83. struct in_addr source_addr)
  84. {
  85. struct listnode *node;
  86. struct channel_oil *c_oil;
  87. for (ALL_LIST_ELEMENTS_RO(qpim_channel_oil_list, node, c_oil)) {
  88. if ((group_addr.s_addr == c_oil->oil.mfcc_mcastgrp.s_addr) &&
  89. (source_addr.s_addr == c_oil->oil.mfcc_origin.s_addr))
  90. return c_oil;
  91. }
  92. return 0;
  93. }
  94. struct channel_oil *pim_channel_oil_add(struct in_addr group_addr,
  95. struct in_addr source_addr,
  96. int input_vif_index)
  97. {
  98. struct channel_oil *c_oil;
  99. c_oil = pim_find_channel_oil(group_addr, source_addr);
  100. if (c_oil) {
  101. ++c_oil->oil_ref_count;
  102. return c_oil;
  103. }
  104. return pim_add_channel_oil(group_addr, source_addr, input_vif_index);
  105. }
  106. void pim_channel_oil_del(struct channel_oil *c_oil)
  107. {
  108. --c_oil->oil_ref_count;
  109. if (c_oil->oil_ref_count < 1) {
  110. pim_channel_oil_delete(c_oil);
  111. }
  112. }