| 1 | /* |
| 2 | * connection.c |
| 3 | * |
| 4 | * Babeltrace Connection |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "CONNECTION" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/graph/notification-iterator-internal.h> |
| 33 | #include <babeltrace/graph/component-internal.h> |
| 34 | #include <babeltrace/graph/component-source-internal.h> |
| 35 | #include <babeltrace/graph/component-filter-internal.h> |
| 36 | #include <babeltrace/graph/connection-internal.h> |
| 37 | #include <babeltrace/graph/private-connection.h> |
| 38 | #include <babeltrace/graph/graph-internal.h> |
| 39 | #include <babeltrace/graph/port-internal.h> |
| 40 | #include <babeltrace/object-internal.h> |
| 41 | #include <babeltrace/compiler-internal.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <glib.h> |
| 44 | |
| 45 | static |
| 46 | void bt_connection_destroy(struct bt_object *obj) |
| 47 | { |
| 48 | struct bt_connection *connection = container_of(obj, |
| 49 | struct bt_connection, base); |
| 50 | |
| 51 | BT_LOGD("Destroying connection: addr=%p", connection); |
| 52 | |
| 53 | /* |
| 54 | * Make sure that each notification iterator which was created |
| 55 | * for this connection is finalized before we destroy it. Once a |
| 56 | * notification iterator is finalized, all its method return |
| 57 | * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status. |
| 58 | * |
| 59 | * Because connections are destroyed before components within a |
| 60 | * graph, this ensures that notification iterators are always |
| 61 | * finalized before their upstream component. |
| 62 | * |
| 63 | * Ending the connection does exactly this. We pass `false` to |
| 64 | * bt_connection_end() here to avoid removing this connection |
| 65 | * from the graph: if we're here, we're already in the graph's |
| 66 | * destructor. |
| 67 | */ |
| 68 | bt_connection_end(connection, false); |
| 69 | g_ptr_array_free(connection->iterators, TRUE); |
| 70 | |
| 71 | /* |
| 72 | * No bt_put on ports as a connection only holds _weak_ |
| 73 | * references to them. |
| 74 | */ |
| 75 | g_free(connection); |
| 76 | } |
| 77 | |
| 78 | static |
| 79 | void bt_connection_try_remove_from_graph(struct bt_connection *connection) |
| 80 | { |
| 81 | void *graph = bt_object_borrow_parent(&connection->base); |
| 82 | |
| 83 | if (connection->base.ref_count.count > 0 || |
| 84 | connection->downstream_port || |
| 85 | connection->upstream_port || |
| 86 | connection->iterators->len > 0) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * At this point we know that: |
| 92 | * |
| 93 | * 1. The connection is ended (ports were disconnected). |
| 94 | * 2. All the notification iterators that this connection |
| 95 | * created, if any, are finalized. |
| 96 | * 3. The connection's reference count is 0, so only the |
| 97 | * parent (graph) owns this connection after this call. |
| 98 | * |
| 99 | * In other words, no other object than the graph knows this |
| 100 | * connection. |
| 101 | * |
| 102 | * It is safe to remove the connection from the graph, therefore |
| 103 | * destroying it. |
| 104 | */ |
| 105 | BT_LOGD("Removing self from graph's connections: " |
| 106 | "graph-addr=%p, conn-addr=%p", graph, connection); |
| 107 | bt_graph_remove_connection(graph, connection); |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | void bt_connection_parent_is_owner(struct bt_object *obj) |
| 112 | { |
| 113 | struct bt_connection *connection = container_of(obj, |
| 114 | struct bt_connection, base); |
| 115 | |
| 116 | bt_connection_try_remove_from_graph(connection); |
| 117 | } |
| 118 | |
| 119 | struct bt_connection *bt_connection_from_private( |
| 120 | struct bt_private_connection *private_connection) |
| 121 | { |
| 122 | return bt_get(bt_connection_borrow_from_private(private_connection)); |
| 123 | } |
| 124 | |
| 125 | BT_HIDDEN |
| 126 | struct bt_connection *bt_connection_create( |
| 127 | struct bt_graph *graph, |
| 128 | struct bt_port *upstream_port, |
| 129 | struct bt_port *downstream_port) |
| 130 | { |
| 131 | struct bt_connection *connection = NULL; |
| 132 | |
| 133 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
| 134 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
| 135 | goto end; |
| 136 | } |
| 137 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
| 138 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | BT_LOGD("Creating connection: " |
| 143 | "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", " |
| 144 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 145 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 146 | downstream_port, bt_port_get_name(downstream_port)); |
| 147 | connection = g_new0(struct bt_connection, 1); |
| 148 | if (!connection) { |
| 149 | BT_LOGE_STR("Failed to allocate one connection."); |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | bt_object_init(connection, bt_connection_destroy); |
| 154 | bt_object_set_parent_is_owner_listener(connection, |
| 155 | bt_connection_parent_is_owner); |
| 156 | connection->iterators = g_ptr_array_new(); |
| 157 | if (!connection->iterators) { |
| 158 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 159 | BT_PUT(connection); |
| 160 | goto end; |
| 161 | } |
| 162 | |
| 163 | /* Weak references are taken, see comment in header. */ |
| 164 | connection->upstream_port = upstream_port; |
| 165 | connection->downstream_port = downstream_port; |
| 166 | BT_LOGD_STR("Setting upstream port's connection."); |
| 167 | bt_port_set_connection(upstream_port, connection); |
| 168 | BT_LOGD_STR("Setting downstream port's connection."); |
| 169 | bt_port_set_connection(downstream_port, connection); |
| 170 | bt_object_set_parent(connection, &graph->base); |
| 171 | BT_LOGD("Created connection: " |
| 172 | "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", " |
| 173 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " |
| 174 | "conn-addr=%p", |
| 175 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 176 | downstream_port, bt_port_get_name(downstream_port), |
| 177 | connection); |
| 178 | |
| 179 | end: |
| 180 | return connection; |
| 181 | } |
| 182 | |
| 183 | BT_HIDDEN |
| 184 | void bt_connection_end(struct bt_connection *conn, |
| 185 | bool try_remove_from_graph) |
| 186 | { |
| 187 | struct bt_component *downstream_comp = NULL; |
| 188 | struct bt_component *upstream_comp = NULL; |
| 189 | struct bt_port *downstream_port = conn->downstream_port; |
| 190 | struct bt_port *upstream_port = conn->upstream_port; |
| 191 | struct bt_graph *graph = bt_connection_borrow_graph(conn); |
| 192 | size_t i; |
| 193 | |
| 194 | BT_LOGD("Ending connection: conn-addr=%p, try-remove-from-graph=%d", |
| 195 | conn, try_remove_from_graph); |
| 196 | |
| 197 | if (downstream_port) { |
| 198 | BT_LOGD("Disconnecting connection's downstream port: " |
| 199 | "port-addr=%p, port-name=\"%s\"", |
| 200 | downstream_port, bt_port_get_name(downstream_port)); |
| 201 | downstream_comp = bt_port_get_component(downstream_port); |
| 202 | bt_port_set_connection(downstream_port, NULL); |
| 203 | conn->downstream_port = NULL; |
| 204 | } |
| 205 | |
| 206 | if (upstream_port) { |
| 207 | BT_LOGD("Disconnecting connection's upstream port: " |
| 208 | "port-addr=%p, port-name=\"%s\"", |
| 209 | upstream_port, bt_port_get_name(upstream_port)); |
| 210 | upstream_comp = bt_port_get_component(upstream_port); |
| 211 | bt_port_set_connection(upstream_port, NULL); |
| 212 | conn->upstream_port = NULL; |
| 213 | } |
| 214 | |
| 215 | if (downstream_comp) { |
| 216 | /* bt_component_port_disconnected() logs details */ |
| 217 | bt_component_port_disconnected(downstream_comp, |
| 218 | downstream_port); |
| 219 | } |
| 220 | |
| 221 | if (upstream_comp) { |
| 222 | /* bt_component_port_disconnected() logs details */ |
| 223 | bt_component_port_disconnected(upstream_comp, upstream_port); |
| 224 | } |
| 225 | |
| 226 | assert(graph); |
| 227 | /* bt_graph_notify_ports_disconnected() logs details */ |
| 228 | bt_graph_notify_ports_disconnected(graph, upstream_comp, |
| 229 | downstream_comp, upstream_port, downstream_port); |
| 230 | bt_put(downstream_comp); |
| 231 | bt_put(upstream_comp); |
| 232 | |
| 233 | /* |
| 234 | * Because this connection is ended, finalize (cancel) each |
| 235 | * notification iterator created from it. |
| 236 | */ |
| 237 | for (i = 0; i < conn->iterators->len; i++) { |
| 238 | struct bt_notification_iterator_private_connection *iterator = |
| 239 | g_ptr_array_index(conn->iterators, i); |
| 240 | |
| 241 | BT_LOGD("Finalizing notification iterator created by this ended connection: " |
| 242 | "conn-addr=%p, iter-addr=%p", conn, iterator); |
| 243 | bt_private_connection_notification_iterator_finalize(iterator); |
| 244 | |
| 245 | /* |
| 246 | * Make sure this iterator does not try to remove itself |
| 247 | * from this connection's iterators on destruction |
| 248 | * because this connection won't exist anymore. |
| 249 | */ |
| 250 | bt_private_connection_notification_iterator_set_connection( |
| 251 | iterator, NULL); |
| 252 | } |
| 253 | |
| 254 | g_ptr_array_set_size(conn->iterators, 0); |
| 255 | |
| 256 | if (try_remove_from_graph) { |
| 257 | bt_connection_try_remove_from_graph(conn); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | struct bt_port *bt_connection_get_upstream_port( |
| 262 | struct bt_connection *connection) |
| 263 | { |
| 264 | return connection ? bt_get(connection->upstream_port) : NULL; |
| 265 | } |
| 266 | |
| 267 | struct bt_port *bt_connection_get_downstream_port( |
| 268 | struct bt_connection *connection) |
| 269 | { |
| 270 | return connection ? bt_get(connection->downstream_port) : NULL; |
| 271 | } |
| 272 | |
| 273 | enum bt_connection_status |
| 274 | bt_private_connection_create_notification_iterator( |
| 275 | struct bt_private_connection *private_connection, |
| 276 | const enum bt_notification_type *notification_types, |
| 277 | struct bt_notification_iterator **user_iterator) |
| 278 | { |
| 279 | enum bt_component_class_type upstream_comp_class_type; |
| 280 | struct bt_notification_iterator_private_connection *iterator = NULL; |
| 281 | struct bt_port *upstream_port = NULL; |
| 282 | struct bt_component *upstream_component = NULL; |
| 283 | struct bt_component_class *upstream_comp_class = NULL; |
| 284 | struct bt_connection *connection = NULL; |
| 285 | bt_component_class_notification_iterator_init_method init_method = NULL; |
| 286 | enum bt_connection_status status; |
| 287 | static const enum bt_notification_type all_notif_types[] = { |
| 288 | BT_NOTIFICATION_TYPE_ALL, |
| 289 | BT_NOTIFICATION_TYPE_SENTINEL, |
| 290 | }; |
| 291 | |
| 292 | if (!private_connection) { |
| 293 | BT_LOGW_STR("Invalid parameter: private connection is NULL."); |
| 294 | status = BT_CONNECTION_STATUS_INVALID; |
| 295 | goto end; |
| 296 | } |
| 297 | |
| 298 | if (!user_iterator) { |
| 299 | BT_LOGW_STR("Invalid parameter: notification iterator pointer is NULL."); |
| 300 | status = BT_CONNECTION_STATUS_INVALID; |
| 301 | goto end; |
| 302 | } |
| 303 | |
| 304 | connection = bt_connection_borrow_from_private(private_connection); |
| 305 | |
| 306 | if (bt_graph_is_canceled(bt_connection_borrow_graph(connection))) { |
| 307 | BT_LOGW("Cannot create notification iterator from connection: " |
| 308 | "connection's graph is canceled: " |
| 309 | "conn-addr=%p, upstream-port-addr=%p, " |
| 310 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " |
| 311 | "upstream-comp-name=\"%s\", graph-addr=%p", |
| 312 | connection, connection->upstream_port, |
| 313 | bt_port_get_name(connection->upstream_port), |
| 314 | upstream_component, |
| 315 | bt_component_get_name(upstream_component), |
| 316 | bt_connection_borrow_graph(connection)); |
| 317 | status = BT_CONNECTION_STATUS_GRAPH_IS_CANCELED; |
| 318 | goto end; |
| 319 | } |
| 320 | |
| 321 | if (bt_connection_is_ended(connection)) { |
| 322 | BT_LOGW("Invalid parameter: connection is ended: " |
| 323 | "conn-addr=%p", connection); |
| 324 | status = BT_CONNECTION_STATUS_IS_ENDED; |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | if (!notification_types) { |
| 329 | BT_LOGD_STR("No notification types: subscribing to all notifications."); |
| 330 | notification_types = all_notif_types; |
| 331 | } |
| 332 | |
| 333 | upstream_port = connection->upstream_port; |
| 334 | assert(upstream_port); |
| 335 | upstream_component = bt_port_get_component(upstream_port); |
| 336 | assert(upstream_component); |
| 337 | upstream_comp_class = upstream_component->class; |
| 338 | BT_LOGD("Creating notification iterator from connection: " |
| 339 | "conn-addr=%p, upstream-port-addr=%p, " |
| 340 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " |
| 341 | "upstream-comp-name=\"%s\"", |
| 342 | connection, connection->upstream_port, |
| 343 | bt_port_get_name(connection->upstream_port), |
| 344 | upstream_component, bt_component_get_name(upstream_component)); |
| 345 | upstream_comp_class_type = |
| 346 | bt_component_get_class_type(upstream_component); |
| 347 | assert(upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 348 | upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_FILTER); |
| 349 | status = bt_private_connection_notification_iterator_create(upstream_component, |
| 350 | upstream_port, notification_types, connection, &iterator); |
| 351 | if (status != BT_CONNECTION_STATUS_OK) { |
| 352 | BT_LOGW("Cannot create notification iterator from connection."); |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | switch (upstream_comp_class_type) { |
| 357 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 358 | { |
| 359 | struct bt_component_class_source *source_class = |
| 360 | container_of(upstream_comp_class, |
| 361 | struct bt_component_class_source, parent); |
| 362 | init_method = source_class->methods.iterator.init; |
| 363 | break; |
| 364 | } |
| 365 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 366 | { |
| 367 | struct bt_component_class_filter *filter_class = |
| 368 | container_of(upstream_comp_class, |
| 369 | struct bt_component_class_filter, parent); |
| 370 | init_method = filter_class->methods.iterator.init; |
| 371 | break; |
| 372 | } |
| 373 | default: |
| 374 | /* Unreachable. */ |
| 375 | BT_LOGF("Unknown component class type: type=%d", |
| 376 | upstream_comp_class_type); |
| 377 | abort(); |
| 378 | } |
| 379 | |
| 380 | if (init_method) { |
| 381 | enum bt_notification_iterator_status iter_status; |
| 382 | |
| 383 | BT_LOGD("Calling user's initialization method: iter-addr=%p", |
| 384 | iterator); |
| 385 | iter_status = init_method( |
| 386 | bt_private_connection_private_notification_iterator_from_notification_iterator((void *) iterator), |
| 387 | bt_private_port_from_port(upstream_port)); |
| 388 | BT_LOGD("User method returned: status=%s", |
| 389 | bt_notification_iterator_status_string(iter_status)); |
| 390 | if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 391 | BT_LOGW_STR("Initialization method failed."); |
| 392 | status = bt_connection_status_from_notification_iterator_status( |
| 393 | iter_status); |
| 394 | goto end; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE; |
| 399 | g_ptr_array_add(connection->iterators, iterator); |
| 400 | BT_LOGD("Created notification iterator from connection: " |
| 401 | "conn-addr=%p, upstream-port-addr=%p, " |
| 402 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " |
| 403 | "upstream-comp-name=\"%s\", iter-addr=%p", |
| 404 | connection, connection->upstream_port, |
| 405 | bt_port_get_name(connection->upstream_port), |
| 406 | upstream_component, bt_component_get_name(upstream_component), |
| 407 | iterator); |
| 408 | |
| 409 | /* Move reference to user */ |
| 410 | *user_iterator = (void *) iterator; |
| 411 | iterator = NULL; |
| 412 | |
| 413 | end: |
| 414 | bt_put(upstream_component); |
| 415 | bt_put(iterator); |
| 416 | return status; |
| 417 | } |
| 418 | |
| 419 | BT_HIDDEN |
| 420 | void bt_connection_remove_iterator(struct bt_connection *conn, |
| 421 | struct bt_notification_iterator_private_connection *iterator) |
| 422 | { |
| 423 | g_ptr_array_remove(conn->iterators, iterator); |
| 424 | BT_LOGV("Removed notification iterator from connection: " |
| 425 | "conn-addr=%p, iter-addr=%p", conn, iterator); |
| 426 | bt_connection_try_remove_from_graph(conn); |
| 427 | } |
| 428 | |
| 429 | bt_bool bt_connection_is_ended(struct bt_connection *connection) |
| 430 | { |
| 431 | return !connection->downstream_port && !connection->upstream_port; |
| 432 | } |