The Flexible Encapsulation (FlexEncap) feature is used in conjunction with pseudowire, L2, and L3 subinterfaces to match on incoming VLAN tags and retain and/or rewrite them on egress. In the case where VLAN tags are swapped or pushed, the class of service (CoS) field of any new VLAN tag is set based on the configured traffic-class to CoS mapping. That is, based on the traffic class the incoming packet traversed through, the CoS of all VLAN tags of the outgoing packet is determined by the result of the traffic-class to CoS map.