| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Geneviève Bastien - Initial implementation and API |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.lttng2.kernel.core.event.matching; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.TcpEventStrings; |
| 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 22 | import org.eclipse.linuxtools.tmf.core.event.matching.ITmfNetworkMatchDefinition; |
| 23 | import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching.MatchingType; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.matching.TmfNetworkEventMatching.Direction; |
| 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.TmfEventTypeCollectionHelper; |
| 27 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace; |
| 28 | |
| 29 | import com.google.common.collect.ImmutableSet; |
| 30 | |
| 31 | /** |
| 32 | * Class to match tcp type events. This matching class applies to traces |
| 33 | * obtained with the 'addons' lttng module. This module can be obtained with |
| 34 | * lttng-modules to generate traces at |
| 35 | * https://github.com/giraldeau/lttng-modules/tree/addons |
| 36 | * |
| 37 | * Note: this module only allows to generate traces to be read and analyzed by |
| 38 | * TMF, no code from this module is being used here |
| 39 | * |
| 40 | * @author Geneviève Bastien |
| 41 | * @since 3.0 |
| 42 | */ |
| 43 | public class TcpEventMatching implements ITmfNetworkMatchDefinition { |
| 44 | |
| 45 | private static final ImmutableSet<String> REQUIRED_EVENTS = ImmutableSet.of( |
| 46 | TcpEventStrings.INET_SOCK_LOCAL_IN, |
| 47 | TcpEventStrings.INET_SOCK_LOCAL_OUT); |
| 48 | |
| 49 | private static boolean canMatchPacket(final ITmfEvent event) { |
| 50 | /* Make sure all required fields are present to match with this event */ |
| 51 | ITmfEventField content = event.getContent(); |
| 52 | if ((content.getField(TcpEventStrings.SEQ) != null) && |
| 53 | (content.getField(TcpEventStrings.ACKSEQ) != null) && (content.getField(TcpEventStrings.FLAGS) != null)) { |
| 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Direction getDirection(ITmfEvent event) { |
| 61 | String evname = event.getType().getName(); |
| 62 | |
| 63 | if (!canMatchPacket(event)) { |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | /* Is the event a tcp socket in or out event */ |
| 68 | if (evname.equals(TcpEventStrings.INET_SOCK_LOCAL_IN)) { |
| 69 | return Direction.IN; |
| 70 | } else if (evname.equals(TcpEventStrings.INET_SOCK_LOCAL_OUT)) { |
| 71 | return Direction.OUT; |
| 72 | } |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * The key to uniquely identify a TCP packet depends on many fields. This |
| 78 | * method computes the key for a given event. |
| 79 | * |
| 80 | * @param event |
| 81 | * The event for which to compute the key |
| 82 | * @return the unique key for this event |
| 83 | */ |
| 84 | @Override |
| 85 | public List<Object> getUniqueField(ITmfEvent event) { |
| 86 | List<Object> keys = new ArrayList<>(); |
| 87 | |
| 88 | keys.add(event.getContent().getField(TcpEventStrings.SEQ).getValue()); |
| 89 | keys.add(event.getContent().getField(TcpEventStrings.ACKSEQ).getValue()); |
| 90 | keys.add(event.getContent().getField(TcpEventStrings.FLAGS).getValue()); |
| 91 | |
| 92 | return keys; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean canMatchTrace(ITmfTrace trace) { |
| 97 | if (!(trace instanceof CtfTmfTrace)) { |
| 98 | return false; |
| 99 | } |
| 100 | CtfTmfTrace ktrace = (CtfTmfTrace) trace; |
| 101 | |
| 102 | Set<String> traceEvents = TmfEventTypeCollectionHelper.getEventNames(ktrace.getContainedEventTypes()); |
| 103 | traceEvents.retainAll(REQUIRED_EVENTS); |
| 104 | return !traceEvents.isEmpty(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public MatchingType[] getApplicableMatchingTypes() { |
| 109 | MatchingType[] types = { MatchingType.NETWORK }; |
| 110 | return types; |
| 111 | } |
| 112 | |
| 113 | } |